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

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

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

注意:

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


当前回答

打开一个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中输入单词“谷歌”,并让它在函数中执行代码!

其他回答

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

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

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

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

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

伊势美元概要文件。CurrentUserAllHosts

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

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

Set-ExecutionPolicy -Scope CurrentUser

然后:

RemoteSigned

然后再次执行以下命令:

伊势美元概要文件。CurrentUserAllHosts

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

这是个人喜好的问题,但我更喜欢将我的别名存储在一个单独的文件中,并在概要文件中调用Import-Alias。

$profileDir = Split-Path $PROFILE -Parent
$profileFile = Join-Path $profileDir profile.ps1
$aliasFile = Join-Path $profileDir aliases.csv
New-Alias -Name npp -Value "C:\Program Files\Notepad++\notepad++.exe" -Description "Notepad++"
Export-Alias -Name npp -Path $aliasFile -Append
ise $profileFile

然后在ISE中,将这一行放到你的profile.ps1中

Import-Alias -Path (Join-Path $PSScriptRoot aliases.csv)

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

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部分,勾选“更改执行策略…”选项,然后应用。