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

如何在PowerShell中创建路径变量并用于新建目录

PowerShell路径变量使用问题解决

问题原因

你使用单引号'$path\New_folder'时,PowerShell会将其视为字面量字符串,不会解析其中的$path变量,导致实际执行时路径是无效的$path\New_folder而非预期的D:\Test\New_folder。

正确写法

方法1:双引号解析变量

用双引号包裹路径字符串,PowerShell会自动解析其中的变量:

$path='D:\Test'
New-Item -Path "$path\New_folder" -ItemType Directory

方法2:字符串拼接

使用+连接变量和子路径,用括号确保优先级:

$path='D:\Test'
New-Item -Path ($path + '\New_folder') -ItemType Directory

方法3:推荐使用Join-Path

Join-Path是PowerShell专门处理路径拼接的cmdlet,会自动处理路径分隔符问题,更可靠:

$path='D:\Test'
$fullPath = Join-Path -Path $path -ChildPath 'New_folder'
New-Item -Path $fullPath -ItemType Directory

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 21:15:35