以下の内容はhttps://tech.guitarrapc.com/entry/2013/09/23/164357より取得しました。


PowerShell で Profile を利用して スクリプトの自動読み込みをしてみよう

PowerShellを起動したら、特定のコマンドを自動実行してほしい。

いわゆる、bashなどでいうbash_profile的なモノです。PowerShellにもprofileな考えは存在していますので、紹介しましょう。

参考

MSDN先生は偉大です。

Windows PowerShell Profiles

現在のProfile確認

powershellやpowershell_ise.exeを起動して、以下のコマンドを入力すると読み込まれているProfileがわかります。

$profile

しかしここに出るのは、現在のProfileで自分のユーザーでのみ適用されるものです。 もう少し詳細を見てみましょう。

Profileと適用対象

Profileは、適用対象応じて以下のつのタイプが存在します。

例えばそのユーザーのPowerShellとPowerShell_iseで、共通のprofileを読み込みたい場合は、 $env:USERPROFILE\Documents\WindowsPowerShell\profile.ps1を作成して、処理を記述します。

ユーザー単位

  • 基底パス : $env:USERPROFILE\Documents\WindowsPowerShell
  • パス例 : C:\Users\hoge\Documents\WindowsPowerShell
適用対象 PowerShell PowerShell ISE 両方
ファイル PowerShell_profile.ps1 Microsoft.PowerShellISE_profile.ps1 Profile.ps1

システム単位

  • 基底パス : $PSHOME
  • パス例 : C:\Windows\System32\WindowsPowerShell\v1.0
適用対象 PowerShell PowerShell ISE 両方
ファイル PowerShell_profile.ps1 Microsoft.PowerShellISE_profile.ps1 Profile.ps1

適用可能なprofileパスの確認

上記の適用可能なパスは、以下のコマンドで確認が可能です。

$profile | Get-Member | where MemberType -eq NoteProperty

結果は、PowerShell.exeとPowerShell_ise.exeで微妙に異なります。

  • PowerShell.exeにおけるprofile
PS> $profile | Get-Member | where MemberType -eq NoteProperty | Format-List


TypeName   : System.String
Name       : AllUsersAllHosts
MemberType : NoteProperty
Definition : System.String AllUsersAllHosts=C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1

TypeName   : System.String
Name       : AllUsersCurrentHost
MemberType : NoteProperty
Definition : System.String AllUsersCurrentHost=C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShellISE_profile.ps1

TypeName   : System.String
Name       : CurrentUserAllHosts
MemberType : NoteProperty
Definition : System.String CurrentUserAllHosts=C:\Users\acquire\Documents\WindowsPowerShell\profile.ps1

TypeName   : System.String
Name       : CurrentUserCurrentHost
MemberType : NoteProperty
Definition : System.String CurrentUserCurrentHost=C:\Users\acquire\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
  • PowerShell_ise.exeにおけるprofile
PS> $profile | Get-Member | where MemberType -eq NoteProperty | Format-List

TypeName   : System.String
Name       : AllUsersAllHosts
MemberType : NoteProperty
Definition : System.String AllUsersAllHosts=C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1

TypeName   : System.String
Name       : AllUsersCurrentHost
MemberType : NoteProperty
Definition : System.String AllUsersCurrentHost=C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1

TypeName   : System.String
Name       : CurrentUserAllHosts
MemberType : NoteProperty
Definition : System.String CurrentUserAllHosts=C:\Users\acquire\Documents\WindowsPowerShell\profile.ps1

TypeName   : System.String
Name       : CurrentUserCurrentHost
MemberType : NoteProperty
Definition : System.String CurrentUserCurrentHost=C:\Users\acquire\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

権限に気を付けよう

いずれのProfileを読み込むにしても、.ps1を読み込むための権限は必須です。

そのため、もし初めてPowerShellを起動する人は、以下のコマンドを管理者で実行しておくとProfileの.ps1を読み込み可能です。

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

profile の作成

よくある紹介で、以下のコマンドでProfileを作成するというのがあります。

if (-not(Test-Path $profile))
{
    New-Item -Path $profile -ItemType file
}

しかし$Profileとやると、Powershell.exePowerShell_ise.exeによって、生成されるProfileが変わります。 そのため、特にPowershell.exePowerShell_ise.exeで分ける必要がない場合は、以下でprofile.ps1を作るほうが楽でしょう。

if (-not(Test-Path $profile.CurrentUserAllHosts))
{
    New-Item -Path $profile.CurrentUserAllHosts -ItemType file
}

Profile でスクリプトを読ませる

作成した、Profileに以下を入力すると、PowerShell.exeとPowerShell_ise.exeの両方で起動時に文字が表示します。

Write-Host ("Hello PS Profile from {0}" -f $PSCommandPath) -ForegroundColor Green

スクリプトの統合

Profileはいくつも種類があります。

と、いうことは各Profileにスクリプト設定が分散しえます。

そのため、どのProfileでも、直接Profileに書くのではなく、読み込むファイルを実行するスクリプトへ流して読み込み先を統合するのも1つの手です。

例えば、私がやる場合は、Profileには以下の一文だけにします。

※ Profileに読み込ませるスクリプトへのパスは、dot-sourceで読み込むように指示します。

$loadscript = "${env:PSCustomScripts}\Profile\PowerShellEnvironment.ps1"
if (Test-Path $loadscript)
{
    .$loadscript
}

あとは、PowerShellEnvironment.ps1に以下を入力して、autoloadフォルダにおいた.ps1だけdot-sourceで読み込ませます。

$psdir="$(env:PSCustomScripts)"
Get-ChildItem $psdir\autoload | where Extension -eq ".ps1" | %{.$_}

Write-Host "Custom PowerShell Environment Loaded" -ForegroundColor Cyan

このようにすることで、自動読み込みする.ps1を統合、管理しやすくできます。

Profile モジュールはいりますか?

Profileの作成、テスト、ファイル取得、削除、バックアップ、編集をするためのModuleを作ったのでよろしければどうぞ。

guitarrapc/PowerShellUtil - PS-Profile | GitHub

最後に

Profileをうまく使えば、 Windows Server 2012などで、毎回Import-Module特定のWindowsFeatureをする必要がなくなります。 ぜひ活用されるとPowerShellが楽になるでしょう。




以上の内容はhttps://tech.guitarrapc.com/entry/2013/09/23/164357より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14