即使我制作了ps1文件也无法保存新别名



>我使用了命令:

Get-Variable profile | Format-List

我这样做是为了检查我的PowerShell配置文件文件夹在哪里。在我这样做之后,它向我展示了这条路径:

C:UsersDomDocumentsWindowsPowerShellMicrosoft.PowerShellISE_profile.ps1

我打开了这个路径和名为 Microsoft.PowerShellISE_profile.ps1 的文件,我在其中使用 command: 保存了我的别名:New-Alias rc Restart-Computer但这个别名不起作用。Powershell的行为就像这个文件从未存在过一样。有什么建议吗?

此脚本将通过当前主机的 4 个配置文件查找$Search,并使用 4 行上下文打印查找结果:

## Search-PSProfiles.ps1
$ProfileNames = $PROFILE | Get-Member -MemberType noteproperty | select -Expandproperty Name
$Search = 'Restart'
ForEach ($Prof in $ProfileNames){
  $ThisProfile = $Profile."$Prof"
  if (Test-Path $ThisProfile){
  "{0,25} search for $Search in $ThisProfile " -f "`$Profile.$Prof" 
    Get-Content $ThisProfile|Select-String -AllMatches $Search -Context 0,4
  } else {
  "{0,25} doesn't exist ({1})" -f "`$Profile.$Prof",$ThisProfile
  }
}

从 PoSh 控制台开始的示例输出:

$Profile.AllUsersAllHosts doesn't exist (C:WindowsSystem32WindowsPowerShellv1.0profile.ps1)
$Profile.AllUsersCurrentHost doesn't exist (C:WindowsSystem32WindowsPowerShellv1.0Microsoft.PowerShell_profile.ps1)
$Profile.CurrentUserAllHosts search for restart in C:UsersUserNameDocumentsWindowsPowerShellprofile.ps1
> New-Alias rc Restart-Computer
$Profile.CurrentUserCurrentHost search for restart in C:UsersUserNameDocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1

相关内容

最新更新