You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PowerShell控制台中为数组元素输出添加双引号

PowerShell输出数组元素时添加双引号的解决方法

你的问题出在Write-Host语句的引号使用错误,导致数组变量没有被正确解析,而是直接输出了字符串"$FileNames[$i]"。下面是几种可行的修正方案:

方案1:字符串插值(推荐)

用双引号包裹整个输出字符串,内部需要显示的双引号用**反引号**转义,同时用$()`确保数组索引表达式被正确求值:

Write-Host "=> Dropped file:- `"$($FileNames[$i])`" successfully into the folder"

方案2:格式化字符串

使用-f运算符格式化输出,可读性更强:

Write-Host "=> Dropped file:- `" {0} `" successfully into the folder" -f $FileNames[$i]

完整修正后的代码

另外建议用Join-Path拼接路径,避免手动拼接时出现路径分隔符缺失的问题:

$FileNames = @(Get-ChildItem $TestFolder\*.txt | Select-Object -ExpandProperty Name) 

For($i=0;$i -lt $FileNames.count;$i++)
{    
    if($FileNames[$i] -eq $fileName)
    {
        $filePathNew = Join-Path -Path $TestFolder -ChildPath $FileNames[$i]
        
        Copy-Item -Path $filePathNew -Destination (Join-Path $RemoteURLFolder $FolderName) -Recurse 
        Write-Host "=> Dropped file:- `"$($FileNames[$i])`" successfully into the folder"
    }
}

内容的提问来源于stack exchange,提问作者user12606498

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 06:07:43