一般情形下所設定的alias 其存續期間僅在此session內
因此關掉視窗之後alias則不存在
若想要保留此alias則需將其設定於Profile檔
<註>自行建立的function,variable也是如此

 

  • Profile4
    • %windir%\system32\WindowsPowerShell\v1.0\profile.ps1
      This profile applies to all users and all shells.
    • %windir%\system32\WindowsPowerShell\v1.0\ Microsoft.PowerShell_profile.ps1
      This profile applies to all users, but only to the Microsoft.PowerShell shell.
    • %UserProfile%\My Documents\WindowsPowerShell\profile.ps1
      This profile applies only to the to the current user, but affects all shells.
    • %UserProfile%\\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
      This profile applies only to the current user and the Microsoft.PowerShell shell.

 

執行
PS C:\ayo>$profile

可得到
C:\ayo\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
因此得知$profile是設定目前使用者有關PowerShell設定的profile檔

此檔案預設是沒有產生的
我們可以透過以下指令檢查
PS C:\Documents and Settings\ayo>Test-Path $profile

得到
False              

False代表檔案不存在 ; True代表存在


我們用以下指令產生該檔案
PS C:\ayo> new-item -path $profile -itemtype file -force

此時執行以下指令則會得到True的結果
PS C:\ayo> Test-Path $profile

使用Get-Content指令檢視檔案內容,發現檔案預設為空的
PS C:\ayo> Get-Content $profile

我們使用Add-content 指令將建立alias的指令寫到檔案中
PS C:\ayo>Add-Content $profile -Value "set-alias gs get-service"

重新啟動PowerShell後執行gs即可得到與get-service相同的結果
PS C:\ayo>gs

因此日後該使用者使用PowerShell時即可使用gs作為get-serviece的alias

(若想要選擇不同的profile則在new-item時建立於所需的檔案路徑)
(若想要使用記事本編輯該檔案 可執行 PS C:\ayo>notepad $profile)

arrow
arrow
    全站熱搜

    ayowu 發表在 痞客邦 留言(0) 人氣()