PowerShell New-Item 将创建脚本所在的目录,而不是指定的路径
PowerShell New-Item will create the directory where the script is and not in the path specified
我正在编写一个 PowerShell 脚本,我需要在 C:\Users
中创建一个目录
我的脚本位于桌面上。
我创建目录的脚本行是这样的:
New-Item -ItemType "directory" -Force -Path "C:Users\username\scripts\documents"
而是创建目录:
C:\Users\username\Desktop\Users\username\scripts\documents
如何直接在C:\Users
中新建目录?
路径根(驱动器盘符)后缺少反斜杠 \
:
New-Item -ItemType "directory" -Force -Path "C:\Users\username\scripts\documents"
另请参阅:https://docs.microsoft.com/en-us/dotnet/api/system.io.path.ispathrooted?view=net-5.0
A rooted path is file path that is fixed to a specific drive or UNC
path; it contrasts with a path that is relative to the current drive
or working directory. For example, on Windows systems, a rooted path
begins with a backslash (for example, \Documents
) or a drive letter
and colon (for example, C:Documents
).
Note that rooted paths can be either absolute (that is, fully
qualified) or relative. An absolute rooted path is a fully qualified
path from the root of a drive to a specific directory. A relative
rooted path specifies a drive, but its fully qualified path is
resolved against the current directory. The following example
illustrates the difference.
$relative1 = "C:Documents"
$relative2 = "\Documents"
$absolute = "C:\Documents"
foreach ($p in $relative1,$relative2,$absolute) {
"'$p' is Rooted: {0}" -f [System.IO.Path]::IsPathRooted($p)
"Full path of '$p' is: {0}" -f [System.IO.Path]::GetFullPath($p)
}
(我没有在链接示例中包含 [System.IO.Path]::IsPathFullyQualified()
调用,因为它不包含在 Windows PowerShell 5.1 中)
我正在编写一个 PowerShell 脚本,我需要在 C:\Users
我的脚本位于桌面上。
我创建目录的脚本行是这样的:
New-Item -ItemType "directory" -Force -Path "C:Users\username\scripts\documents"
而是创建目录:
C:\Users\username\Desktop\Users\username\scripts\documents
如何直接在C:\Users
中新建目录?
路径根(驱动器盘符)后缺少反斜杠 \
:
New-Item -ItemType "directory" -Force -Path "C:\Users\username\scripts\documents"
另请参阅:https://docs.microsoft.com/en-us/dotnet/api/system.io.path.ispathrooted?view=net-5.0
A rooted path is file path that is fixed to a specific drive or UNC path; it contrasts with a path that is relative to the current drive or working directory. For example, on Windows systems, a rooted path begins with a backslash (for example,
\Documents
) or a drive letter and colon (for example,C:Documents
).Note that rooted paths can be either absolute (that is, fully qualified) or relative. An absolute rooted path is a fully qualified path from the root of a drive to a specific directory. A relative rooted path specifies a drive, but its fully qualified path is resolved against the current directory. The following example illustrates the difference.
$relative1 = "C:Documents"
$relative2 = "\Documents"
$absolute = "C:\Documents"
foreach ($p in $relative1,$relative2,$absolute) {
"'$p' is Rooted: {0}" -f [System.IO.Path]::IsPathRooted($p)
"Full path of '$p' is: {0}" -f [System.IO.Path]::GetFullPath($p)
}
(我没有在链接示例中包含 [System.IO.Path]::IsPathFullyQualified()
调用,因为它不包含在 Windows PowerShell 5.1 中)