File name without extension

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?
Thanks

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