INDEX
Windows PowerShell Tips
Windows PowerShell Tips
ウインドウタイトル変更
コマンドプロンプトでの TITLE コマンドの様に、コンソール ウインド のタイトルを設定する。
[System.Console]::Title = "新しいタイトル"
ビープ音
Console.Beep
Console クラスの Beep メソッド。
# デフォルト設定でビープ音を鳴らす [Console]::Beep() # 指定の周波数(37 - 32767 Hz)、再生時間(ミリ秒単位)でビープ音鳴らす [Console]::Beep(523, 1000)
ベル
ベル文字(0x07, ^G または、文字定数 \a )を出力し、「システムエラー」のサウンドを鳴らす。
Write-Output `a Write-Output "`a"
tail -f
Get-Content -Path "ファイルパス" -wait -tail 10
gc "ファイルパス" -wait -tail 10
touch
空ファイルを作成する
ni "ファイルパス"
New-Item -ItemType file -Path "ファイルパス"
ファイルの日付を変更する
(Get-Item "ファイルパス").LastWriteTime = (Get-Date)
(Get-Item "ファイルパス").LastWriteTime = "2021/01/01 12:34:56"
デスクトップにショートカットを作成
REM %INSTDIR% インストールディレクトリ(ワークディレクトリ)
REM %EXECNAME% 実行ファイル名
REM %LINKNAME% ショートカット名
powershell -Command $wsh=New-Object -ComObject WScript.Shell;$lnk=$wsh.CreateShortcut(${Home}+'\Desktop\%LINKNAME%.lnk');$lnk.TargetPath='%INSTDIR%\%EXECNAME%';$lnk.WorkingDirectory='%INSTDIR%';$lnk.Save();
ファイルのダウンロード
powershell -Command $wc=New-Object System.Net.WebClient;$wc.DownloadFile('ダウンロードURL', '保存ファイル');
シャットダウン・再起動
シャットダウン
Stop-Computer
強制的に停止する場合は「-Force」、待機時間を設ける場合は「-Timeout 300」と待つ秒数を指定する。
再起動
Restart-Computer
Stop-Computer と同様に、Force, Timeout オプションも可能。
スリープ・休止状態
スリープ・休止状態に移行するコマンドレットは用意されていない。
Application.SetSuspendState メソッドを利用する。引数は、移行後の電源の動作モードを示す PowerState で Suspend(0) か Hibernate(1) 。2つ目は、強制的に中断モードにするか(true/false)。3つ目は、wake イベントでアクティブにならないようにするか(true/false)。
Add-Type -AssemblyName System.Windows.Forms;[System.Windows.Forms.Application]::SetSuspendState([System.Windows.Forms.PowerState]::Hibernate, $false, $false) Add-Type -AssemblyName System.Windows.Forms;[System.Windows.Forms.Application]::SetSuspendState([System.Windows.Forms.PowerState]::Suspend, $true, $false) # enum, boolean なので省略して Add-Type -AssemblyName System.Windows.Forms;[System.Windows.Forms.Application]::SetSuspendState(0,1,0);
※休止状態が無効で、Hibernate を指定しても、エラーにならずスリープに移行される?
Windows PowerShell Tips
特定のポートの疎通確認
Test-NetConnection コマンドレット(エイリアス TNC)で、ICMP または TCPの任意のポートでの疎通確認ができる。Windows Server 2012 R2 では、“Test-Connection”となる模様。
Test-NetConnection [[-ComputerName] <String>] [-InformationLevel {Quiet | Detailed}] [-Port <Int32>] [-TraceRoute]
ComputerName を指定しなかった場合は、マイクロソフト社が用意したインターネットに接続できるかの確認用ホスト internetbeacon.msedge.net が対象になる。
Port でポート番号を指定すると TCP のポートで確認となり、未指定の場合は ICMP (いわゆる ping )での確認になる。
InformationLevel で Quiet を指定すると TcpTestSucceeded の結果 True/False のみが表示される。
TraceRoute を指定すると、tracert (traceroute) コマンドの様に、対象ホストまでの経路が表示される。
- Test-NetConnection (NetTCPIP) | Microsoft Docs
自己署名証明書
New-SelfSignedCertificate コマンドレットで長期有効期間のオレオレ証明書(自己署名証明書)を作成。また、作成した証明書をpfxファイルにエクスポートする。
$thisCert = New-SelfSignedCertificate -CertStoreLocation "cert:\LocalMachine\My" ` -FriendlyName "myserver" ` -Subject "myserver.mynetwork.local" -DnsName "myserver.mynetwork.local","192.0.2.1" ` -KeyAlgorithm RSA -KeyLength 2048 -HashAlgorithm SHA256 -KeyExportPolicy Exportable ` -NotAfter (Get-Date).AddYears(10) [String]$thisCertPath = Join-Path -Path 'cert:\LocalMachine\My\' -ChildPath "$($thisCert.Thumbprint)" $thisCertPwd = ConvertTo-SecureString -String "cert password" -Force -AsPlainText Export-PfxCertificate -Cert $thisCertPath -FilePath 'ThisServerCert.pfx' -Password $thisCertPwd
カレントディレクトリに ThisServerCert.pfx と言うファイルでエクスポートされる。また、作成された証明書は、ローカルコンピューターの証明書マネージャー (certlm.msc) の「証明書 - ローカルコンピュータ」-「個人」-「証明書」内にも保存される。
- New-SelfSignedCertificate (pki) | Microsoft Docs
New-SelfSignedCertificate コマンドレットは、Windows Server 2012 だとダメ? 2016 では作成できる。
最終更新時間:2026年05月17日 14時11分04秒 指摘や意見などあればSandBoxのBBSへ。