New-PSDrive
创建临时和永久性的映射网络驱动器。
语法
New-PSDrive
[-Name] <String>
[-PSProvider] <String>
[-Root] <String>
[-Description <String>]
[-Scope <String>]
[-Persist]
[-Credential <PSCredential>]
[-WhatIf]
[-Confirm]
[-UseTransaction]
[<CommonParameters>]
说明
New-PSDrive
cmdlet 创建映射到或与数据存储中某个位置关联的临时和永久性驱动器,例如网络驱动器、本地计算机上的目录或注册表项,以及与远程计算机上的文件系统位置关联的持久 Windows 映射网络驱动器。
临时驱动器仅存在于当前 PowerShell 会话和你在当前会话中创建的会话中。
它们可以具有在 PowerShell 中有效的任何名称,并且可以映射到任何本地或远程资源。
可以使用临时 PowerShell 驱动器访问关联数据存储中的数据,就像使用任何映射的网络驱动器一样。
可以使用 Set-Location
、cd或 chdir将位置更改为驱动器,并使用 Get-Item
、Get-ChildItem
或 dir访问驱动器的内容。
但是,由于临时驱动器仅对 PowerShell 已知,因此无法使用文件资源管理器、Windows Management Instrumentation(WMI)、组件对象模型(COM)或 Microsoft .NET Framework 或使用 Net Use 等工具访问它们。
以下功能添加到 Windows PowerShell 3.0 中的 New-PSDrive
:
- 映射的网络驱动器。
可以使用
New-PSDrive
的 Persist 参数来创建 Windows 映射网络驱动器。 与临时 PowerShell 驱动器不同,Windows 映射的网络驱动器不是特定于会话的。 它们保存在 Windows 中,可以使用标准 Windows 工具(如文件资源管理器和 Net Use)进行管理。 映射的网络驱动器必须具有驱动器号名称,并连接到远程文件系统位置。 当命令在本地范围(无点溯)时,Persist 参数不会保留创建 PSDrive 超出运行命令的范围。 如果在脚本中运行New-PSDrive
,并且希望驱动器无限期保留,则必须对脚本进行点源。 为了获得最佳结果,若要强制新驱动器无限期保留,请将 Scope 参数添加到命令,并将其值设置为 Global。 - 外部驱动器。 当外部驱动器连接到计算机时,PowerShell 会自动向表示新驱动器的文件系统添加 PSDrive。 无需重启 PowerShell。 同样,当外部驱动器与计算机断开连接时,PowerShell 会自动删除表示已删除驱动器的 PSDrive。
- UNC 路径的凭据。
当 Root 参数的值是 UNC 路径(如 \\Server\Share)时,Credential 参数的值中指定的凭据用于创建 PSDrive。 否则,创建新文件系统驱动器时凭据 无效。
示例
示例 1:创建映射到网络共享的驱动器
New-PSDrive -Name "P" -PSProvider "FileSystem" -Root "\\Server01\Public"
Name Provider Root
---- -------- ----
P FileSystem \\Server01\Public
此命令创建一个名为 P 的临时 PowerShell 驱动器:映射到 \\Server01\Public network 共享。
它使用 Name 参数指定驱动器的名称、PSProvider 参数来指定 PowerShell FileSystem 提供程序,以及用于指定网络共享的 Root 参数。
命令完成后,\\Server01\Public 共享的内容将显示在 P: 驱动器中。
若要查看它们,请键入:dir P:
。
示例 2:创建临时驱动器
New-PSDrive -Name MyDocs -PSProvider FileSystem -Root "C:\Documents and Settings\User01\My Documents" -Description "Maps to my My Documents folder."
Name Provider Root
---- -------- ----
MyDocs FileSystem C:\Documents and Settings\User01\My Documents
此命令创建一个临时 PowerShell 驱动器,该驱动器提供对本地目录的快速访问。 它创建一个名为 MyDocs 的驱动器:该驱动器映射到本地计算机上的“C:\Documents and Settings\User01\My Documents”目录。
它使用 名称 指定驱动器的名称,PSProvider 指定 PowerShell FileSystem 提供程序,根 指定“我的文档”文件夹的路径,Description 参数来创建驱动器的说明。
命令完成后,“我的文档”文件夹的内容将显示在 MyDocs:
驱动器中。
若要查看它们,请键入:dir MyDocs:
。
示例 3:为注册表项创建驱动器
New-PSDrive -Name "MyCompany" -PSProvider "Registry" -Root "HKLM:\Software\MyCompany"
Name Provider Root
---- -------- ----
MyCompany Registry HKEY_LOCAL_MACHINE\Software\MyCo...
此命令创建一个临时 PowerShell 驱动器,用于快速访问经常检查的注册表项。
它创建一个名为 MyCompany 的驱动器,该驱动器映射到 HKLM\Software\MyCompany
注册表项。
它使用 名称 指定驱动器的名称,PSProvider 指定 PowerShell 注册表提供程序,根 指定注册表项。
命令完成后,MyCompany 密钥的内容将显示在 MyCompany:
驱动器中。
若要查看它们,请键入:dir MyCompany:
。
示例 4:创建持久映射的网络驱动器
New-PSDrive -Name "S" -Root "\\Server01\Scripts" -Persist -PSProvider "FileSystem"
Net Use
Status Local Remote Network
---------------------------------------------------------
OK S: \\Server01\Scripts Microsoft Windows Network
此命令在本地计算机上创建“S”映射的网络驱动器。 “S”驱动器映射到 \\Server01\Scripts 网络共享。
该命令使用 New-PSDrive
创建映射的网络驱动器。
它使用 持久化 创建保存在本地计算机上的 Windows 映射网络驱动器。
该命令使用 名称 来指定 Windows 接受的字母名称,并 根 指定远程计算机上的位置。 它使用 PSProvider 来指定 FileSystem 提供程序。
可以在本地计算机上的其他 PowerShell 会话、Windows 资源管理器和其他工具(如 Net Use)中查看生成的驱动器。
示例 5:创建持久性驱动器和临时驱动器
# Create a temporary PowerShell drive called PSDrive: that is 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-WmiObject 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-WmiObject Win32_LogicalDisk | Format-Table -Property DeviceID
Get-WmiObject 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
此示例显示了永久性映射网络驱动器与映射到同一网络共享的临时 PowerShell 驱动器之间的差异。
如果关闭 PowerShell 会话,然后打开一个新会话,则 PSDrive:驱动器已消失,X:
驱动器仍然存在。 因此,在确定用于映射网络驱动器的方法时,请考虑如何使用驱动器、驱动器是否必须持久以及驱动器是否必须对其他 Windows 功能可见。
参数
-Confirm
在运行 cmdlet 之前,提示你进行确认。
类型: | SwitchParameter |
别名: | cf |
Position: | Named |
默认值: | False |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-Credential
指定有权执行此操作的用户帐户。 默认值为当前用户。
键入用户名(如“User01”或“Domain01\User01”),或输入 PSCredential 对象,例如由 Get-Credential
cmdlet 生成的用户名。
如果键入用户名,此 cmdlet 会提示输入密码。
从 Windows PowerShell 3.0 开始,当 Root 参数的值是 UNC 路径时,可以使用凭据创建文件系统驱动器。 所有 PowerShell 提供程序都不支持此参数。
类型: | 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 映射网络驱动器。 映射的网络驱动器保存在本地计算机上的 Windows 中。 它们是永久性的,而不是特定于会话的,可以在文件资源管理器和其他工具中查看和管理它们。
在本地限定命令范围时(即没有点溯),Persist 参数不会保留创建 PSDrive 超出运行命令的范围。
如果在脚本中运行 New-PSDrive
,并且希望新驱动器无限期保留,则必须对脚本进行点源。
为了获得最佳结果,若要强制保留新驱动器,请将 Global 指定为 Scope 参数的值,同时将 Persist 添加到命令。
驱动器的名称必须是字母,例如“D”或“E”。 Root 参数的值必须是其他计算机的 UNC 路径。 PSProvider 参数的值必须是 FileSystem。
若要断开 Windows 映射网络驱动器的连接,请使用 Remove-PSDrive
cmdlet。
断开 Windows 映射网络驱动器的连接时,映射将从计算机永久删除,而不仅仅是从当前会话中删除。
映射的网络驱动器特定于用户帐户。 使用其他用户凭据在提升的会话或会话中创建的映射驱动器在会话中不可见,这些驱动器使用不同的凭据启动。
类型: | SwitchParameter |
Position: | Named |
默认值: | False |
必需: | False |
接受管道输入: | True |
接受通配符: | False |
-PSProvider
指定支持此类驱动器的 PowerShell 提供程序。
例如,如果驱动器与网络共享或文件系统目录相关联,则 PowerShell 提供程序为 FileSystem。 如果驱动器与注册表项相关联,则提供程序为注册表。
临时 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 |
-UseTransaction
在活动事务中包含该命令。 此参数仅在事务正在进行时有效。 有关详细信息,请参阅 about_Transactions。
类型: | SwitchParameter |
别名: | usetx |
Position: | Named |
默认值: | False |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-WhatIf
显示 cmdlet 运行时会发生什么情况。 cmdlet 未运行。
类型: | SwitchParameter |
别名: | wi |
Position: | Named |
默认值: | False |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
输入
None
不能通过管道将输入传递给此 cmdlet。
输出
备注
-
New-PSDrive
旨在处理任何提供程序公开的数据。 若要列出会话中可用的提供程序,请使用Get-PSProvider
。 有关提供程序的详细信息,请参阅 about_Providers。 - 映射的网络驱动器特定于用户帐户。 使用其他用户凭据在提升的会话或会话中创建的映射驱动器在会话中不可见,这些驱动器使用不同的凭据启动。