New-PSDrive
创建与项数据存储中某个位置关联的临时和永久性驱动器。
语法
New-PSDrive
[-Name] <String>
[-PSProvider] <String>
[-Root] <String>
[-Description <String>]
[-Scope <String>]
[-Persist]
[-Credential <PSCredential>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
说明
New-PSDrive
cmdlet 创建映射到或与数据存储中某个位置关联的临时和永久性驱动器,例如网络驱动器、本地计算机上的目录或注册表项,以及与远程计算机上的文件系统位置关联的持久 Windows 映射网络驱动器。
临时驱动器仅存在于当前 PowerShell 会话和你在当前会话中创建的会话中。 它们可以具有在 PowerShell 中有效的任何名称,并且可以映射到任何本地或远程资源。 可以使用临时 PowerShell 驱动器访问关联数据存储中的数据,就像使用任何映射的网络驱动器一样。 可以使用 Set-Location
将位置更改为驱动器,并使用 Get-Item
或 Get-ChildItem
访问驱动器的内容。
由于临时驱动器仅对 PowerShell 已知,因此无法使用文件资源管理器、Windows Management Instrumentation(WMI)、组件对象模型(COM)、Microsoft .NET Framework 或 net use
等工具访问它们。
以下功能已添加到 PowerShell 3.0 中的 New-PSDrive
:
- 映射的网络驱动器。 可以使用
New-PSDrive
的 Persist 参数来创建 Windows 映射网络驱动器。 与临时 PowerShell 驱动器不同,Windows 映射的网络驱动器不是特定于会话的。 它们保存在 Windows 中,可以使用标准 Windows 工具(如文件资源管理器和 net use)进行管理。 映射的网络驱动器必须具有驱动器号名称,并连接到远程文件系统位置。 在本地限定命令范围时,没有点溯,Persist 参数不会保留创建 PSDrive 超出运行命令的范围。 如果要在脚本中运行New-PSDrive
,并且希望驱动器无限期保留,则必须对脚本进行点源。 为了获得最佳结果,若要强制新驱动器无限期保留,请将 Scope 参数添加到命令,并将其值设置为 Global。 有关点溯的详细信息,请参阅 about_Scripts。 - 外部驱动器。 当外部驱动器连接到计算机时,PowerShell 会自动向表示新驱动器的文件系统添加 PSDrive。 无需重启 PowerShell。 同样,当外部驱动器与计算机断开连接时,PowerShell 会自动删除表示已删除驱动器的 PSDrive。
- 通用命名约定 (UNC) 路径的凭据。
当 Root 参数的值是 UNC 路径(如 \\Server\Share
)时,Credential 参数的值中指定的凭据用于创建 PSDrive。 否则,创建新文件系统驱动器时,凭据 无效。
一些代码示例使用喷洒来减少行长并提高可读性。 有关详细信息,请参阅 about_Splatting。
注意
除非使用 Scope 参数,否则 PSDrive 是在运行 New-PSDrive
命令的作用域中创建的。
示例
示例 1:创建映射到网络共享的临时驱动器
此示例创建映射到网络共享的临时 PowerShell 驱动器。
New-PSDrive -Name "Public" -PSProvider "FileSystem" -Root "\\Server01\Public"
Name Provider Root
---- -------- ----
Public FileSystem \\Server01\Public
New-PSDrive
使用 Name 参数指定名为 Public
的 PowerShell 驱动器和 PSProvider 参数来指定 PowerShell FileSystem
提供程序。
Root 参数指定网络共享的 UNC 路径。
若要查看 PowerShell 会话中的内容:Get-ChildItem -Path Public:
示例 2:创建映射到本地目录的临时驱动器
此示例创建一个临时 PowerShell 驱动器,该驱动器提供对本地计算机上的目录的访问权限。
$parameters = @{
Name = "MyDocs"
PSProvider = "FileSystem"
Root = "C:\Users\User01\Documents"
Description = "Maps to my My Documents folder."
}
New-PSDrive @parameters
Name Provider Root
---- -------- ----
MyDocs FileSystem C:\Users\User01\Documents
Splatting 创建参数键和值。
Name 参数指定驱动器名称,MyDocs。
PSProvider 参数指定 PowerShell FileSystem
提供程序。
根 指定本地计算机的目录。
Description 参数描述驱动器的目的。
New-PSDrive
使用 splatted 参数创建 MyDocs
驱动器。
若要查看 PowerShell 会话中的内容:Get-ChildItem -Path MyDocs:
示例 3:为注册表项创建临时驱动器
此示例创建一个临时 PowerShell 驱动器,该驱动器提供对注册表项的访问权限。 它创建一个名为 MyCompany 的驱动器,该驱动器映射到 HKLM:\Software\MyCompany
注册表项。
New-PSDrive -Name "MyCompany" -PSProvider "Registry" -Root "HKLM:\Software\MyCompany"
Name Provider Root
---- -------- ----
MyCompany Registry HKLM:\Software\MyCompany
New-PSDrive
使用 Name 参数指定名为 MyCompany
的 PowerShell 驱动器和 PSProvider 参数来指定 PowerShell Registry
提供程序。
Root 参数指定注册表位置。
若要查看 PowerShell 会话中的内容:Get-ChildItem -Path MyCompany:
示例 4:使用凭据创建持久性映射网络驱动器
此示例映射使用域服务帐户凭据进行身份验证的网络驱动器。 有关存储凭据的 PSCredential 对象以及如何将密码存储为 SecureString的详细信息,请参阅 Credential 参数的说明。
$cred = Get-Credential -Credential Contoso\ServiceAccount
New-PSDrive -Name "S" -Root "\\Server01\Scripts" -Persist -PSProvider "FileSystem" -Credential $cred
Net Use
Status Local Remote Network
---------------------------------------------------------
OK S: \\Server01\Scripts Microsoft Windows Network
注意
请记住,如果在脚本中使用上述代码片段,请将 Scope 参数值设置为“全局”,以确保驱动器在当前作用域之外保留。
$cred
变量存储包含服务帐户凭据的 PSCredential 对象。
Get-Credential
提示输入存储在 SecureString中的密码。
New-PSDrive
使用多个参数创建映射的网络驱动器。
名称 指定 Windows 接受的 S
驱动器号。
根 将 \\Server01\Scripts
定义为远程计算机上的位置。
持久保存 创建保存在本地计算机上的 Windows 映射网络驱动器。
PSProvider 指定 FileSystem
提供程序。
凭据 使用 $cred
变量获取服务帐户凭据进行身份验证。
可以在 PowerShell 会话、文件资源管理器中的本地计算机上查看映射的驱动器,并使用 net 使用等工具。 若要查看 PowerShell 会话中的内容:Get-ChildItem -Path S:
示例 5:创建持久性驱动器和临时驱动器
此示例显示了永久性映射网络驱动器与映射到同一网络共享的临时 PowerShell 驱动器之间的差异。
如果关闭 PowerShell 会话,然后打开新会话,则临时 PSDrive:
不可用,但永久性 X:
驱动器可用。 确定用于映射网络驱动器的方法时,请考虑如何使用驱动器。 例如,它是否必须持久,以及驱动器是否必须对其他 Windows 功能可见。
# Create a temporary PowerShell drive called PSDrive:
# that's mapped to the \\Server01\Public network share.
New-PSDrive -Name "PSDrive" -PSProvider "FileSystem" -Root "\\Server01\Public"
# Use the Persist parameter of New-PSDrive to create the X: mapped network drive,
# which is also mapped to the \\Server01\Public network share.
New-PSDrive -Persist -Name "X" -PSProvider "FileSystem" -Root "\\Server01\Public"
# Now, you can use the Get-PSDrive drive cmdlet to examine the two drives.
# The drives appear to be the same, although the network share name appears only
# in the root of the PSDrive: drive.
Get-PSDrive -Name "PSDrive", "X"
Name Provider Root
---- -------- ----
PsDrive FileSystem \\Server01\public
X FileSystem X:\
# Get-Member cmdlet shows that the drives have the same object type,
# System.Management.Automation.PSDriveInfo.
Get-PSDrive "PSDrive", "x" | Get-Member
TypeName: System.Management.Automation.PSDriveInfo
Name MemberType Definition
---- ---------- ----------
CompareTo Method System.Int32 CompareTo(PSDriveInfo drive),
Equals Method System.Boolean Equals(Object obj),
GetHashCode Method System.Int32 GetHashCode()
...
# Net Use and Get-CimInstance for the Win32_LogicalDisk class,
# and Win32_NetworkConnection class find only the persistent X: drive.
# PowerShell temporary drives are known only to PowerShell.
Net Use
Get-CimInstance Win32_LogicalDisk | Format-Table -Property DeviceID
Get-CimInstance Win32_NetworkConnection
Status Local Remote Network
--------------------------------------------------------
OK X: \\contoso-pc\data Microsoft Windows Network
deviceid
--------
C:
D:
X:
LocalName RemoteName ConnectionState Status
--------- ---------- --------------- ------
X: \\products\public Disconnected Unavailable
示例 6:在脚本中创建永久性驱动器
PSDrive 是在运行 New-PSDrive
命令的作用域中创建的。 在脚本中运行命令时,驱动器映射是脚本的本地映射。 脚本退出时,驱动器不再可用。
New-PSDrive -Persist -Name "X" -PSProvider "FileSystem" -Root "\\Server01\Public" -Scope Global
若要确保驱动器在脚本外部可用,必须使用 Scope 参数在 全局 范围内创建驱动器。
参数
-Confirm
在运行 cmdlet 之前,提示你进行确认。
类型: | SwitchParameter |
别名: | cf |
Position: | Named |
默认值: | False |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-Credential
指定有权执行此作的用户帐户。 默认值为当前用户。
由于 PowerShell 3.0,当 Root 参数的值是 UNC 路径时,可以使用凭据创建文件系统驱动器。
键入用户名(如 User01 或 Domain01\User01),或输入由 Get-Credential
cmdlet 生成的 PSCredential 对象。 如果键入用户名,系统会提示输入密码。
凭据存储在 PSCredential 对象中,密码存储为 SecureString。
注意
有关 SecureString 数据保护的详细信息,请参阅 SecureString 的安全性如何?。
类型: | PSCredential |
Position: | Named |
默认值: | Current user |
必需: | False |
接受管道输入: | True |
接受通配符: | False |
-Description
指定驱动器的简短文本说明。 键入任何字符串。
若要查看所有会话驱动器的说明,Get-PSDrive | Format-Table Name, Description
。
若要查看特定驱动器的说明,请键入 (Get-PSDrive <DriveName>).Description
。
类型: | String |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | True |
接受通配符: | False |
-Name
指定新驱动器的名称。 对于持久性映射网络驱动器,请使用驱动器号。 对于临时 PowerShell 驱动器,不限于驱动器号,请使用任何有效的字符串。
类型: | String |
Position: | 0 |
默认值: | None |
必需: | True |
接受管道输入: | True |
接受通配符: | False |
-Persist
指示此 cmdlet 创建 Windows 映射网络驱动器。 Persist 参数仅在 Windows 上可用。
映射的网络驱动器保存在本地计算机上的 Windows 中。 它们是永久性的,不是特定于会话的,可以在文件资源管理器和其他工具中查看和管理它们。
在本地限定命令范围时,Persist 参数不会保留创建 PSDrive 超出运行命令的范围。 如果在脚本中运行 New-PSDrive
,并且希望新驱动器无限期保留,则必须对脚本进行点源。 为了获得最佳结果,若要强制新驱动器保留,请将 全局 指定为 Scope 参数的值,并在命令中包含 Persist。
驱动器的名称必须是字母,例如 D
或 E
。
Root 参数的值必须是其他计算机的 UNC 路径。
PSProvider 参数的值必须 FileSystem
。
若要断开 Windows 映射网络驱动器的连接,请使用 Remove-PSDrive
cmdlet。 断开 Windows 映射网络驱动器的连接时,映射将从计算机永久删除,而不仅仅是从当前会话中删除。
映射的网络驱动器特定于用户帐户。 使用其他用户凭据在提升的会话或会话中创建的映射驱动器在使用不同凭据启动的会话中不可见。
类型: | SwitchParameter |
Position: | Named |
默认值: | False |
必需: | False |
接受管道输入: | True |
接受通配符: | False |
-PSProvider
指定支持此类驱动器的 PowerShell 提供程序。
例如,如果驱动器与网络共享或文件系统目录相关联,则 PowerShell 提供程序 FileSystem
。 如果驱动器与注册表项相关联,则提供程序 Registry
。
临时 PowerShell 驱动器可与任何 PowerShell 提供程序相关联。 映射的网络驱动器只能与 FileSystem
提供程序相关联。
若要查看 PowerShell 会话中的提供程序列表,请使用 Get-PSProvider
cmdlet。
类型: | String |
Position: | 1 |
默认值: | None |
必需: | True |
接受管道输入: | True |
接受通配符: | False |
-Root
指定 PowerShell 驱动器映射到的数据存储位置。
例如,指定网络共享,例如 \\Server01\Public
、本地目录(如 C:\Program Files
)或注册表项(如 HKLM:\Software\Microsoft
)。
临时 PowerShell 驱动器可与任何受支持的提供程序驱动器上的本地或远程位置相关联。 映射的网络驱动器只能与远程计算机上的文件系统位置相关联。
类型: | String |
Position: | 2 |
默认值: | None |
必需: | True |
接受管道输入: | True |
接受通配符: | False |
-Scope
指定驱动器的范围。 此参数的可接受值为:全局、本地和 脚本,或相对于当前范围的数字。 范围数字 0 到范围数。 当前范围编号为 0,其父级为 1。 有关详细信息,请参阅 about_Scopes。
类型: | String |
Position: | Named |
默认值: | Local |
必需: | False |
接受管道输入: | True |
接受通配符: | False |
-WhatIf
显示 cmdlet 运行时会发生什么情况。 该 cmdlet 未运行。
类型: | SwitchParameter |
别名: | wi |
Position: | Named |
默认值: | False |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
输入
None
无法通过管道将对象传递给此 cmdlet
输出
此 cmdlet 返回表示创建的驱动器的 PSDriveInfo 对象。
备注
PowerShell 包含以下 Get-PSDrive
别名:
- 所有平台:
ndr
- 窗户:
mount
New-PSDrive
旨在处理任何提供程序公开的数据。 若要列出会话中可用的提供程序,请使用 Get-PSProvider
。 有关提供程序的详细信息,请参阅 about_Providers。
映射的网络驱动器特定于用户帐户。 使用其他用户凭据在提升的会话或会话中创建的映射驱动器在使用不同凭据启动的会话中不可见。