New-PSDrive

建立與專案資料存放區中位置相關聯的暫存和永續性磁片磁碟機。

Syntax

New-PSDrive
   [-Name] <String>
   [-PSProvider] <String>
   [-Root] <String>
   [-Description <String>]
   [-Scope <String>]
   [-Persist]
   [-Credential <PSCredential>]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]

Description

Cmdlet New-PSDrive 會建立與資料存放區中某個位置對應的暫存和永續性磁片磁碟機,例如網路磁碟機機、本機電腦上的目錄或登錄機碼,以及與遠端電腦上檔案系統位置相關聯的持續性 Windows 對應網路磁碟機機。

暫存磁片磁碟機只存在於目前 PowerShell 會話和您在目前會話中建立的會話中。 它們可以具有任何在 PowerShell 中有效的名稱,而且可以對應至任何本機或遠端資源。 您可以使用暫時的 PowerShell 磁片磁碟機來存取相關聯資料存放區中的資料,就像使用任何對應的網路磁碟機機一樣。 您可以使用 將位置變更為磁片磁碟機, Set-Location 並使用 或 Get-ChildItem 存取磁片磁碟機 Get-Item 的內容。

因為只有 PowerShell 才知道暫存磁片磁碟機,所以您無法使用 檔案總管、Windows Management Instrumentation (WMI) 、元件物件模型 (COM) 、Microsoft .NET Framework 或之類的 net use 工具來存取它們。

PowerShell 3.0 中已新增 New-PSDrive 下列功能:

  • 對應的網路磁碟機。 您可以使用 的 New-PSDrivePersist參數來建立 Windows 對應網路磁碟機機。 不同于暫存 PowerShell 磁片磁碟機,Windows 對應網路磁碟機機不是會話特定的磁片磁碟機。 它們會儲存在 Windows 中,而且可以使用標準 Windows 工具來管理,例如檔案總管和net use。 連線的網路磁碟機必須擁有磁碟機代號名稱並連線至遠端檔案系統位置。 當您的命令在本機設定範圍時,沒有點來源, Persist 參數不會在命令執行的範圍之外保存 PSDrive 的建立。 如果您是在腳本內執行 New-PSDrive ,而且希望磁片磁碟機無限期保存,則必須以點來源編寫腳本。 為了獲得最佳結果,若要強制無限期保留新的磁碟機,請在命令中加入 Scope 參數,並將其值設定為 Global。 如需點來源的詳細資訊,請參閱 about_Scripts
  • 外部磁片磁碟機。 當外部磁片磁碟機連線到電腦時,PowerShell 會自動將 PSDrive 新增至代表新磁片磁碟機的檔案系統。 您不需要重新開機 PowerShell。 同樣地,當外部磁片磁碟機與電腦中斷連線時,PowerShell 會自動刪除代表已移除磁片磁碟機的 PSDrive
  • 通用命名慣例的認證 (UNC) 路徑。

Root 參數的值是 UNC 路徑時,例如 \\Server\ShareCredential 參數的值中指定的認證會用來建立 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

展開會建立參數索引鍵和值。 Name參數會指定磁片磁碟機名稱MyDocsPSProvider參數會指定 PowerShell FileSystem 提供者。 Root 指定本機電腦的目錄。 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 參數值設定為 「Global」,以確保磁片磁碟機會保存在目前的範圍之外。

變數 $cred 會儲存包含服務帳戶認證的 PSCredential 物件。 Get-Credential 會提示您輸入儲存在 SecureString中的密碼。

New-PSDrive 使用數個參數建立對應的網路磁碟機機。 名稱 會指定 Windows 接受的 S 磁碟機號。 和 Root\\Server01\Scripts 定義為遠端電腦上的位置。 保存 會建立儲存在本機電腦上的 Windows 對應網路磁碟機機。 PSProviderFileSystem 指定提供者。 認證$cred 使用 變數來取得服務帳戶認證以進行驗證。

您可以在 PowerShell 會話的本機電腦上檢視對應的磁片磁碟機、檔案總管,以及使用 net use之類的工具。 若要從 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 前提示您確認。

Type:SwitchParameter
Aliases:cf
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Credential

指定有權執行此動作的使用者帳戶。 預設為目前使用者。

由於 PowerShell 3.0,當 Root 參數的值是 UNC 路徑時,您可以使用認證來建立檔案系統磁片磁碟機。

輸入使用者名稱,例如User01Domain01\User01,或輸入 Cmdlet 所產生的 Get-CredentialPSCredential物件。 如果您輸入使用者名稱,系統會提示您輸入密碼。

認證會儲存在 PSCredential 物件中,密碼會儲存為 SecureString

注意

如需 SecureString 資料保護的詳細資訊,請參閱 SecureString 有多安全?

Type:PSCredential
Position:Named
Default value:Current user
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-Description

指定磁碟機的簡短文字描述。 輸入任何字串。

若要查看所有會話磁片磁碟機的描述, Get-PSDrive | Format-Table Name, Description

若要查看特定磁片磁碟機的描述,請輸入 (Get-PSDrive <DriveName>).Description

Type:String
Position:Named
Default value:None
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-Name

指定新磁碟機的名稱。 對於持續性的對應網路磁碟機機,請使用磁碟機號。 對於暫時的 PowerShell 磁片磁碟機,您不限於磁碟機號,請使用任何有效的字串。

Type:String
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-Persist

表示此 Cmdlet 會建立 Windows 對應網路磁碟機機。 Persist參數僅適用于 Windows。

連線的網路磁碟機會儲存在本機電腦的 Windows 中。 它們是持續性的,而不是會話特定的,而且可以在檔案總管和其他工具中檢視及管理。

當您在本機設定命令的範圍時,如果沒有點來源, Persist 參數就不會在執行命令的範圍之外保存 PSDrive 的建立。 如果您在腳本內執行 New-PSDrive ,而且您想要讓新磁片磁碟機無限期保存,則必須以點來源編寫腳本。 為了獲得最佳結果,若要強制新磁片磁碟機保存,請將 Global 指定為 Scope 參數的值,並在命令中包含 Persist

磁片磁碟機的名稱必須是字母,例如 DERoot參數的值必須是不同電腦的 UNC 路徑。 PSProvider參數的值必須是 FileSystem

若要中斷 Windows 對應網路磁碟機機的連線,請使用 Remove-PSDrive Cmdlet。 當您中斷 Windows 連線網路磁碟機的連線時,該連線會永久從該電腦上刪除,而不是只從目前的工作階段中刪除。

連線網路磁碟機僅適用於特定使用者帳戶。 使用其他使用者的認證在提升許可權的會話或會話中建立的對應磁片磁碟機不會顯示在使用不同認證啟動的會話中。

Type:SwitchParameter
Position:Named
Default value:False
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-PSProvider

指定支援這種磁片磁碟機的 PowerShell 提供者。

例如,如果磁片磁碟機與網路共用或檔案系統目錄相關聯,則 PowerShell 提供者為 FileSystem 。 如果磁片磁碟機與登錄機碼相關聯,則提供者為 Registry

暫存 PowerShell 磁片磁碟機可以與任何 PowerShell 提供者相關聯。 對應的網路磁碟機機只能與 FileSystem 提供者相關聯。

若要查看 PowerShell 會話中的提供者清單,請使用 Get-PSProvider Cmdlet。

Type:String
Position:1
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-Root

指定 PowerShell 磁片磁碟機對應的資料存放區位置。

例如,指定網路共用,例如 \\Server01\Public 、本機目錄,例如 C:\Program Files 或登錄機碼,例如 HKLM:\Software\Microsoft

暫存 PowerShell 磁片磁碟機可以與任何支援的提供者磁片磁碟機上的本機或遠端位置相關聯。 連線的網路磁碟機只能與遠端電腦上的檔案系統位置關聯。

Type:String
Position:2
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-Scope

指定磁碟機的範圍。 此參數可接受的值為: 全域區域腳本,或相對於目前範圍的數位。 範圍數位 0 到範圍數目。 目前的範圍編號為 0,其父系為 1。 如需詳細資訊,請參閱 about_Scopes

Type:String
Position:Named
Default value:Local
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-WhatIf

顯示執行 Cmdlet 後會發生的情況。 不會執行此 Cmdlet。

Type:SwitchParameter
Aliases:wi
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

輸入

None

您無法將輸入管線傳送至此 Cmdlet。

輸出

PSDriveInfo

備註

New-PSDrive 是設計來使用任何提供者所公開的資料。 若要列出會話中可用的提供者,請使用 Get-PSProvider 。 如需提供者的詳細資訊,請參閱 about_Providers

連線網路磁碟機僅適用於特定使用者帳戶。 使用其他使用者的認證在提升許可權的會話或會話中建立的對應磁片磁碟機不會顯示在使用不同認證啟動的會話中。