Powershell没有';t填充变量

Powershell没有';t填充变量,powershell,Powershell,我有一个Powershell脚本,它被一个包含文件名的参数调用。我想从文件名中删除扩展名。以下是脚本: param([string]$input_filename) $inputFileNameOnly = [System.IO.Path]::GetFileNameWithoutExtension($input_filename) | Out-file "myfile.log" -Append Write-Output "input filename without

我有一个Powershell脚本,它被一个包含文件名的参数调用。我想从文件名中删除扩展名。以下是脚本:

param([string]$input_filename)
$inputFileNameOnly = [System.IO.Path]::GetFileNameWithoutExtension($input_filename) | Out-file "myfile.log" -Append
Write-Output "input filename without extension: " $inputFileNameOnly | Out-file "myfile.log" -Append
当我运行该文件时: \myscript.ps1“E:\Projectdata\Test book.html”

我可以看到调用
[System.IO.Path]::GetFileNameWithoutExtension($input\u filename)
起作用了:我的日志文件中的第一行是“testbook”

但“输入不带扩展名的文件名:”之后没有任何内容。变量$inputFileNameOnly没有赋值

我做错了什么?似乎没有类型不匹配:
[System.IO.Path]::GetFileName WithOutExtension
输出字符串


我正在Windows 10中使用Powershell 5。

您的管道有点太快了:

$inputFileNameOnly = [System.IO.Path]::GetFileNameWithoutExtension($input_filename) | Out-file "myfile.log" -Append
这是一个过程中的两个步骤:
[System.IO.Path]::GetFileNameWithoutExtension($input_filename)| Out file…
将获取您的值并将其写入文件。但是,此操作不会提供可在
$InputFileName Only
中捕获的任何输出
$inputFileNameOnly
$Null

而是先将文件名保存在变量中,然后将其用于
输出文件

$inputFileNameOnly = [System.IO.Path]::GetFileNameWithoutExtension($input_filename) 
Out-file -InputObject $inputFileNameOnly -FilePath "myfile.log" -Append

一些不向管道提供输出的cmdlet具有参数
-PassThru
,以强制它们向管道发送内容。不幸的是,
Out文件
没有。

就是这样。以这种方式使用管道可以在批处理文件中工作(我对批处理文件比powershell更熟悉)。