Powershell 重命名项认为变量是路径

Powershell 重命名项认为变量是路径,powershell,rename,rename-item-cmdlet,Powershell,Rename,Rename Item Cmdlet,我试图重命名文件,但powershell认为我的变量是字符串,因此失败 以下是脚本: $date=(get-date -Format d) $time=(get-date -Format t) $source = "D:\_qapi.log" $newfilename = "$date"+"_"+"$time"+"_qapi[SERVERNAME].log" Rename-Item $source -NewName $newfilename 下面是错误: Rename-Item : Cann

我试图重命名文件,但powershell认为我的变量是字符串,因此失败

以下是脚本:

$date=(get-date -Format d)
$time=(get-date -Format t)
$source = "D:\_qapi.log"
$newfilename = "$date"+"_"+"$time"+"_qapi[SERVERNAME].log"

Rename-Item $source -NewName $newfilename
下面是错误:

Rename-Item : Cannot rename because the target specified represents a path or device name.

有人知道我能解决这个问题吗?出于某种原因,powershell将$newfilename中的$date变量视为路径。

它在日期时间字符串中的非法字符

这项工作:

$date=(get-date -Format d) -replace("/")
$time=(get-date -Format t) -replace(":")
$source = "D:\_qapi.log"
$newfilename = "$date"+"_"+"$time"+"_qapi[$env:Computername].log"

Rename-Item $source -NewName $newfilename