cd ~ 在 PWD 处于注册表中时引发错误



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:UsersAdmin

而对于注册表提供程序,家庭位置为空

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:>

此致敬意

克夫普拉松

最新更新