我想创建一个cmdlet的别名,它不会在我关闭Powershell的当前会话后过期,假设我有这样的别名:

C:\Users\Aymen> New-Alias Goto Set-Location

这完美地创建了Goto别名,但我想使用它,甚至在我关闭当前会话后,我如何实现这一点。

注意:

PowerShell帮助系统建议我可以导出我创建的别名,并在下次我打开一个新会话时导入它们,实际上这并不是我真正在寻找的,是否有一种直接清晰的方法来保持别名在我通过不同的会话创建它之后


当前回答

这有点花哨…但它是有效的:

步骤1:创建Powershell配置文件:

FILE: install_profile.ps1
# THIS SCRIPT BLOWS AWAY YOUR DEFAULT POWERSHELL PROFILE SCRIPT
#   AND INSTALLS A POINTER TO A GLOBAL POWERSHELL PROFILE

$ErrorActionPreference = "Stop"

function print ([string]$msg)
{
    Write-Host -ForegroundColor Green $msg
}

print ""

# User's Powershell Profile
$psdir  = "$env:USERPROFILE\Documents\WindowsPowerShell"
$psfile = $psdir + "\Microsoft.PowerShell_profile.ps1"

print "Creating Directory: $psdir"
md $psdir -ErrorAction SilentlyContinue | out-null

# this is your auto-generated powershell profile to be installed
$content = @(
    "",
    ". ~/Documents/tools/profile.ps1",
    ""
)

print "Creating File: $psfile"
[System.IO.File]::WriteAllLines($psfile, $content)

print ""

# Make sure Powershell profile is readable
Set-ExecutionPolicy -Scope CurrentUser Unrestricted

第二步:打开tools ~/Documents/tools/profile.ps1:

function Do-ActualThing {
    # do actual thing
}

Set-Alias MyAlias Do-ActualThing

步骤3:

$ Set-ExecutionPolicy -Scope CurrentUser无限制 . ./install_profile.ps1

其他回答

我发现我可以运行这个命令:

notepad $PROFILE.CurrentUserAllHosts

它打开我的默认powershell配置文件(当前用户,所有主机)。我在这里找到的。

然后添加别名。例如,这是我的jupyter笔记本的别名jn(我讨厌每次都打印出笨重的jupyter笔记本):

Set-Alias -Name jn -Value C:\Users\words\Anaconda3\Scripts\jupyter-notebook.exe

只是在这个可能的地点列表上加上…

这对我不起作用: 用户\ \{我}\ \ WindowsPowerShell \ Microsoft.PowerShell_profile.ps1文档

然而,这造成了: 用户\ \{我}\ OneDrive \ \ WindowsPowerShell \ Microsoft.PowerShell_profile.ps1文档

如果你没有配置文件,或者你想建立一个,运行下面的命令,它会创建必要的文件夹/文件,甚至告诉你它在哪里! 新建项目-path $profile -type file -force

打开一个Windows PowerShell窗口,输入:

notepad $profile

然后创建一个函数,比如:

function goSomewhereThenOpenGoogleThenDeleteSomething {
    cd C:\Users\
    Start-Process -FilePath "http://www.google.com"
    rm fileName.txt
}

然后在函数名下面输入:

Set-Alias google goSomewhereThenOpenGoogleThenDeleteSomething

现在,您可以在Windows PowerShell中输入单词“谷歌”,并让它在函数中执行代码!

创建profile1。PSL文件,输入以下命令:

新项目概要文件。CurrentUserAllHosts -ItemType file -Force

要访问文件,输入下面的命令:

伊势美元概要文件。CurrentUserAllHosts

注意,如果你以前没有这样做过,你会发现你不会 能够运行脚本是因为您的执行策略,您需要将其从受限(默认)更改为无限制。

为此,关闭脚本,然后键入以下命令:

Set-ExecutionPolicy -Scope CurrentUser

然后:

RemoteSigned

然后再次执行以下命令:

伊势美元概要文件。CurrentUserAllHosts

最后在脚本中输入您的别名,保存它,它们应该 每次运行powershell时都要运行,即使是在重新启动计算机之后。

将这类东西直接添加到$env:WINDIR powershell文件夹中并不是个好主意,除非您确实希望别名是全局的。 建议将其添加到您的个人档案中:

cd $env:USERPROFILE\Documents
md WindowsPowerShell -ErrorAction SilentlyContinue
cd WindowsPowerShell
New-Item Microsoft.PowerShell_profile.ps1 -ItemType "file" -ErrorAction SilentlyContinue
powershell_ise.exe .\Microsoft.PowerShell_profile.ps1

现在将别名添加到Microsoft.PowerShell_profile。现在打开的Ps1文件:

function Do-ActualThing {
    # do actual thing
}

Set-Alias MyAlias Do-ActualThing

然后保存它,并刷新当前会话:

. $profile

注意: 以防万一,如果你得到许可

CategoryInfo: SecurityError: (:) [], PSSecurityException + fullyqualifiederrid: UnauthorizedAccess

尝试下面的命令并再次刷新会话。

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser