我想创建alias
cmdlet
的{{1}},在我关闭当前的Powershell会话后不会过期,让我说我有这个别名:
C:\Users\Aymen> New-Alias Goto Set-Location
这完美地创建了Goto
别名,但我想在关闭当前会话后使用它,我该如何实现呢。
注意:
PowerShell帮助系统建议我可以导出我创建的别名,并在下次打开新会话时导入它们,实际上这并不是我真正想要的,因为,是否有直接通过不同的会话创建别名后,明确的方法是保持别名
答案 0 :(得分:71)
将此类内容直接添加到$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
答案 1 :(得分:63)
更新 - 2017年1月
可以在profile.ps1
文件中存储PowerShell每次启动时要执行的任何powershell代码。至少6 different paths存储代码的位置取决于哪个用户必须执行它。我们只能考虑其中的两个:"所有用户"和#34;只有你的用户"路径。
因此,要回答您的问题,您只需要创建一个包含您要执行的代码的profile.ps1
文件,即
New-Alias Goto Set-Location
并将其保存在正确的路径中:
"$Home\Documents"
(C:\ Users \ yourname \ Documents):只有您的用户才会执行该代码。$PsHome
(C:\ Windows \ System32 \ WindowsPowerShell \ v1.0):每个用户都将执行代码。要应用更改,请关闭所有打开的powershell实例并重新启动它们。
<强> TIPS 强>
如果两个路径都包含profile.ps1
文件,则首先执行所有用户,然后执行特定用户。
如果不需要将代码扩展到每个用户,请始终将代码放在用户特定的配置文件中。您不需要管理员权限即可将文件添加到您的用户空间(否则您会这样做)并且您不会污染其他用户&#39;空间。
记住! PowerShell的32位和64位实例之间的$PsHome
路径不同,因此如果您想要始终执行配置文件代码,则必须考虑这两种环境。通常,64位环境的路径为C:\Windows\System32\WindowsPowerShell\v1.0
,而32位路径的路径为C:\Windows\SysWow64\WindowsPowerShell\v1.0
。
答案 2 :(得分:13)
您可以借助简单的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脚本被阻止。要启用它们,请打开设置 - &gt;更新&amp;安全 - &gt;对于开发者。选择开发者模式(可能需要重启)。
。
向下滚动到PowerShell部分,勾选&#34;更改执行政策......&#34;选项,并申请。
答案 3 :(得分:8)
只是添加到此可能位置列表中......
这对我没用:
\Users\{ME}\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
然而这样做:
\Users\{ME}\OneDrive\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
如果您没有个人资料或者您想要设置个人资料,请运行以下命令,它会创建必要的文件夹/文件,甚至告诉您它的位置!
New-Item -path $profile -type file -force
答案 4 :(得分:6)
简单。例如...
打开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中键入单词“ google”,并使其在您的函数中执行代码!
答案 5 :(得分:5)
创建profile1.psl文件,输入以下命令:
new-item $ PROFILE.CurrentUserAllHosts -ItemType file -Force
要访问该文件,请输入下一个命令:
ise $ PROFILE.CurrentUserAllHosts
note 如果你之前没有这样做,你会发现你不会这样做 能够因执行策略而运行脚本,您需要将其更改为Unrestricted from Restricted(默认)。
执行此操作关闭脚本,然后键入以下命令:
Set-ExecutionPolicy -Scope CurrentUser
然后:
<强>下RemoteSigned 强>
然后再次执行此命令:
ise $ PROFILE.CurrentUserAllHosts
然后最后在脚本中输入别名,保存它们,它们应该 每次运行PowerShell时都会运行,即使在重新启动计算机之后也是如此。
答案 6 :(得分:0)
这有点花哨的东西...但是可以用:
步骤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
第2步:,然后在工具〜/ Documents / tools / profile.ps1中:
function Do-ActualThing {
# do actual thing
}
Set-Alias MyAlias Do-ActualThing
第3步:
$ Set-ExecutionPolicy-作用域CurrentUser不受限制 $。 ./install_profile.ps1
答案 7 :(得分:0)
我发现我可以运行以下命令:
notepad $((Split-Path $profile -Parent) + "\profile.ps1")
,它会打开我的默认Powershell配置文件(至少在使用Windows终端时)。我发现了here。
然后添加一个别名。例如,这是jn
的别名jupyter notebook
(我讨厌每次都键入麻烦的jupyter notebook
):
Set-Alias -Name jn -Value C:\Users\words\Anaconda3\Scripts\jupyter-notebook.exe