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

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

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

注意:

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


当前回答

2018年,Windows 10

您可以在一个简单的PowerShell脚本的帮助下链接到任何文件或目录。

正在编写文件快捷方式脚本

打开Windows PowerShell ISE。在脚本窗格中写入:

New-Alias ${shortcutName} ${fullFileLocation}

然后转到命令行窗格。使用echo $profile查找PowerShell用户配置文件地址。将脚本保存在此地址。

PowerShell配置文件地址中的脚本将在每次打开PowerShell时运行。该快捷方式应该适用于每个新的PowerShell窗口。

正在编写目录快捷方式脚本

它需要我们脚本中的另一行。

function ${nameOfFunction} {set-location ${directory_location}}
New-Alias ${shortcut} ${nameOfFunction} 

其余部分完全相同。

启用PowerShell脚本

默认情况下,PowerShell脚本是阻塞的。要启用它们,请打开设置->更新和安全->用于开发人员。选择开发者模式(可能需要重新启动)。 .

向下滚动到PowerShell部分,勾选“更改执行策略…”选项,然后应用。

其他回答

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

这对我不起作用: 用户\ \{我}\ \ 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中输入单词“谷歌”,并让它在函数中执行代码!

将这类东西直接添加到$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

提供最简单的解决方案:

打开powershell notepad $ phome \ profile .ps1:

函数Goto() { #函数名为别名,命令集为函数体 Set-Location } 3.保存并退出记事本 4. 设置powershell权限:Set-ExecutionPolicy -ExecutionPolicy remotessigned 5. 重新启动powershell并写入goto

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

步骤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