Powershell 当PWD在区域中时,cd~抛出错误

Powershell 当PWD在区域中时,cd~抛出错误,powershell,Powershell,CD~正在抛出以下错误 PS HKLM:\> cd ~ cd : Home location for this provider is not set. To set the home location, call "(get-psprovider 'Registry').Home = 'path'". At line:1 char:1 + cd ~ + ~~~~ + CategoryInfo : InvalidOperation: (:) [Set-Locati

CD~正在抛出以下错误

PS HKLM:\> cd ~
cd : Home location for this provider is not set. To set the home location,     call "(get-psprovider 'Registry').Home = 'path'".
At line:1 char:1
+ cd ~
+ ~~~~
+ CategoryInfo          : InvalidOperation: (:) [Set-Location], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.SetLocationCommand

为什么会这样?

抛出此错误是因为您在注册表中,默认情况下它没有主目录。错误消息明确指出,为了设置主目录(即使我看不出有理由这样做),您需要调用
(get-psprovider'Registry')。home='path'

这是因为在PowerShell中没有为注册表提供程序分配主位置

默认情况下,在PowerShell中,我们有文件系统的主位置,并且将是加载的用户配置文件的主目录

PS C:\> Get-PSProvider -PSProvider FileSystem | Select-Object home

Home
----
C:\Users\Admin
而对于注册表提供程序,主位置为空

PS C:\> Get-PSProvider -PSProvider Registry | Select-Object home

Home
----
但是你可以设定

PS C:\> Get-PSProvider -PSProvider Registry | Get-Member


TypeName: System.Management.Automation.ProviderInfo

Name             MemberType Definition
----             ---------- ----------
Equals           Method     bool Equals(System.Object obj)
GetHashCode      Method     int GetHashCode()
GetType          Method     type GetType()
ToString         Method     string ToString()
Capabilities     Property       System.Management.Automation.Provider.ProviderCapabilities Capabilities {get;}
Description      Property   string Description {get;set;}
Drives           Property   System.Collections.ObjectModel.Collection    [System.Management.Automation.PSDriveInfo] Drives {get;}
HelpFile         Property   string HelpFile {get;}
Home             Property   string Home {get;set;} # **Can Be SET** .
ImplementingType Property   type ImplementingType {get;}
Module           Property   psmoduleinfo Module {get;}
ModuleName       Property   string ModuleName {get;}
Name             Property   string Name {get;}
PSSnapIn         Property   System.Management.Automation.PSSnapInInfo     PSSnapIn {get;}
你可以这样设置

PS C:\> $provider = Get-PSProvider -PSProvider Registry
PS C:\> $provider.Home = "HKLM:\"
PS C:\> Get-PSProvider -PSProvider Registry | Select-Object home

Home
----
HKLM:\

PS C:\> cd HKLM:\SOFTWARE\
PS HKLM:\SOFTWARE\> cd ~
PS HKLM:\>
致以最良好的祝愿

克夫普拉松