剛剛處理過自動化登入VPN 等的 powershell script (ps1), 會使用很多次的命令行, 也要測試自己寫的 ps1 到底效果如何, 所以常要切換到不同的目錄, 這就帶來很多打字或剪貼命令的動作, 有方法簡單一些嗎 ? 有的 Microsoft 有說明,
http://technet.microsoft.com/en-us/library/bb963733.aspx
不過 win7 是行不通的, 原因的是不同版本 WINDOWS, 不同的目錄結構
俺是這樣做的
建立一個資料夾 MyScripts mkdir C:\Users\user0\Documents\MyScripts 切換到資料夾 MyScripts cd C:\Users\user0\Documents\MyScripts 建立 Profile.ps1 在 Profile.ps1 內寫入以下內容 #http://technet.microsoft.com/en-us/library/bb963733.aspx # win7 not working # Filename: Profile.ps1 #$env:path = $env:path + ";<C>:\Documents and Settings\<UserAlias>\My Documents\MyScripts" # end of script 2014-07-11, xiaolaba # win 7, path is different # Filename: Profile.ps1 $env:path = $env:path + ";<C>:\Users\<user0>\Documents\MyScripts" # end of script 然後建立 hello.ps1, 內容如下, # Filename: Hello.ps1 Write-Host Write-Host 'Hello World!' Write-Host "Good-bye World! `n" # end of script 最後試驗執行, 成功就會顯示 Hello World .\hello