File name without extension

Welcome Forums General PowerShell Q&A File name without extension

This topic contains 1 reply, has 2 voices, and was last updated by

3 years, 6 months ago.

  • Author
  • #25665

    Points: 0
    Rank: Member

    Hi developer,

    Here is my sample script:
    Get-Content .\Test.txt | ForEach-Object{$_ -replace "\s+",","} | Out-File -FilePath Test.csv -Encoding OEM

    It is ok as above the script.

    Now i am going to change of my logic script as below:

    $File = .\Test.txt
    $BaseName = $File.BaseName

    Get-Content $File | ForEach-Object{$_ -replace "\s+",","} | Out-File -FilePath $BaseName.csv -Encoding OEM

    it does not work and how to get file name or base name (Test) without extension?

    Any idea?

  • #25669

    Points: 51
    Rank: Member

    Your line retrieving the file is not actually getting the file. I imagine the file is just opening on your screen when you run "$File = .\Test.txt". You have to use Get-Item to get file properties like BaseName.

    $BaseName = Get-Item .\test.txt | Select-Object -ExpandProperty BaseName

The topic ‘File name without extension’ is closed to new replies.