PowerShellコマンド

本項は書きかけの記事です。正確な情報は公式サイト、公式ドキュメント、記載の参照サイトでご確認ください。
OSS(OpenSourceSoftware)を利用しています。使用期限や保守、公開期間の確約がないことに留意してください。

目次

+ 読む

概要

2021年11月、PowerShell 7.2リリースのLTS版。
コマンドはver 5.1以降は約1500を有する(バージョンにより差がある)
.NET 6.0 を使用、Linux 用のユニバーサル インストーラー パッケージを備えています。
PowerShell 7.2 以降への更新は Microsoft Update で提供されます。

対応アプリ

ExchangeServer、SQL Server、SharePointなどのMicrosoftWindows系ミドルウェア系などで利用されている。

管理者権限のショートカット

ショートカットから管理者権限で起動する場合は以下のコマンドを設定する。

Powershell
cmd /c @powershell -NoProfile -ExecutionPolicy unrestricted -Command "Start-Process powershell.exe -Verb runas"
 

コマンドプロンプト
@echo off
echo Set obj = CreateObject("Shell.Application"):obj.ShellExecute "cmd.exe", "", "", "runas", 1 > %TEMP%\_acmd.vbs
cscript %TEMP%\_acmd.vbs
 

ファイル拡張子

Extension Description
.ps1 Script file
.psd1 Module's manifest file; usually comes with a script module or binary module
.psm1 Script module file
.ps1xml Format and type definitions file
.psc1 Console file
.pssc Session configuration file
.psrc Role Capability file


主要コマンドと、コマンドプロンプトとLinuxシェルの対比

PowerShell (Cmdlet) PowerShell (Alias) Windows Command Prompt Unix shell
Get-ChildItem gci, dir, ls dir ls
Test-Connection ping ping ping
Get-Content gc, type, cat type cat
Get-Command gcm help, where type, which, compgen
Get-Help help, man help apropos, man
Clear-Host cls, clear cls clear
Copy-Item cpi, copy, cp copy, xcopy, robocopy cp
Move-Item mi, move, mv move mv
Remove-Item ri, del, erase, rmdir, rd, rm del, erase, rmdir, rd rm, rmdir
Rename-Item rni, ren, mv ren, rename mv
Get-Location gl, cd, pwd cd pwd
Pop-Location popd popd popd
Push-Location pushd pushd pushd
Set-Location sl, cd, chdir cd, chdir cd
Tee-Object tee ? tee
Write-Output echo, write echo echo
Get-Process gps, ps tlist, tasklist ps
Stop-Process spps, kill kill, taskkill kill
Select-String sls find, findstr grep
Set-Variable sv, set set env, export, set, setenv
Invoke-WebRequest iwr, curl, wget curl wget, curl

AliasCmd Original etc command result
sal Set-Alias sal curl (Join-Path $env:ChocolateyInstall "bin\curl.exe")
sal curl (Join-Path $env:ChocolateyInstall "bin\curl.exe") -O AllScope
-
gal Get-Alias - CommandType Name Version Source
Alias curl -> curl.exe
なし cd $env:環境変数値 cd $env:NVM_HOME NVM_HOMEの場所へcd

概要 コマンド 備考
絶対パスでpsファイルを指定 powershell -File D:\path\to\dir\filename.ps1 -
引数あり powershell -File D:\path\to\dir\filename.ps1 10 "あいうえお" 123 -
コマンド直接実行 set Arg1=1234
set Arg2=あいうえお
powershell -Command "Write-Host %Arg1%;Write-Host %Arg2%;"
内部でコーテーション利用時はエスケープが必要

時間、パス出力

日時

日付: Get-Date -Format "yyyyMMdd"
時間: Get-Date -Format "HHmmss"
ミリ秒: Get-Date -Format "fff"

パス

カレントディレクトリ: Get-Location

準備とTips

+ 読む
項目名 更新日 概要 備考、リンク
プロンプト表示パスの調整 2022/1/8 項目名を参照
入力履歴 0108 (Get-PSReadlineOption).HistorySavePath
Get-History

ダブルクリックで起動

1.ショートカットを作る
2.バッチファイルから起動させる
3.レジストリを変更する




PSのeval、invoke-expression

PS D:\> $calc="1+2+3+4+5+6+7+8+9+10"
PS D:\> $calc
1+2+3+4+5+6+7+8+9+10
PS D:\> $calc.GetType()
PS D:\> Invoke-Expression $calc
PS D:\> $target="c:\"
PS D:\> $cmd = "get-childitem $target"
PS D:\> Invoke-Expression $cmd
PS C:\Users\takk> 1..5 -join "+"
1+2+3+4+5
PS C:\Users\takk> 1..5 -join "+" | invoke-expression

参考

管理者コマンドプロンプト/PowerShell起動ショートカット - lisz-works
https://www.lisz-works.com/entry/boot-admin-cmd-ps

タグ:

powershell cmd
最終更新:2023年07月27日 19:49