Skip to content

yzkn/PowerShell-cheatsheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

おまじない

PowerShell実行ポリシーの変更

# 一時的に許可
Set-ExecutionPolicy RemoteSigned -Scope Process -Force

# ユーザーに対して許可
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
実行ポリシー 内容
Restricted 個々のコマンドは許可、スクリプトは制限される
AllSigned 署名されたスクリプトのみ許可
RemoteSigned ローカルのスクリプトは許可、リモートは署名されていれば許可
Unrestricted 全てのスクリプトが制限なし(警告あり)
Bypass 全てのスクリプトが制限なし(警告なし)

署名する

$targetScriptPath = ".\gen.ps1"

# 証明書作成
$cert = New-SelfSignedCertificate -Subject "CN=PS証明書" -KeyAlgorithm RSA -KeyLength 2048 -Type CodeSigningCert -CertStoreLocation Cert:\CurrentUser\My\ -NotAfter ([datetime]"2099/01/01")
Move-Item "Cert:\CurrentUser\My\$($cert.Thumbprint)" Cert:\CurrentUser\Root

# 署名
$cert = (Get-ChildItem Cert:\CurrentUser\My | ? {$_.Subject -eq "CN=PS証明書"})[0]
Set-AuthenticodeSignature -Cert $cert -Filepath $targetScriptPath

PowerShell バージョン

スクリプト内から確認

$PSVersionTable
Name                           Value
----                           -----
PSVersion                      7.2.1
PSEdition                      Core
GitCommitId                    7.2.1
OS                             Microsoft Windows 10.0.22000
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
$PSVersionTable["PSVersion"].Major

# バージョンが7未満ならインストーラーをダウンロードして実行
if ($PSVersionTable["PSVersion"].Major -lt 7) {
    Invoke-Expression "& { $(Invoke-RestMethod https://aka.ms/install-powershell.ps1) } -UseMSI"
    exit
}
7

ターミナルから確認

$ pwsh --version

ウィンドウを閉じない

エクスプローラーから「PowerShellで実行」でps1ファイルを実行した場合には、コマンド実行が完了した後PowerShellのウィンドウが閉じてしまう。閉じないようにするためにはスクリプトファイルの最後に pause を追記しておく。

<#

処理

#>

pause

ヘルプ

# Get-Help <Command>
Get-Help Get-ChildItem

文法

コメント

# コメント

<#

範囲

#>

コマンドレット

Get-Command # コマンドレット一覧
Get-Service # サービス一覧
get-service # 大小文字の区別はない

引数の指定

$iyo = 'i'

Write-Host foo
Write-Host "b a r" # 1つの引数として指定
Write-Host "-hoge" # オプションではなく文字列として指定
Write-Host p$iyo   # 変数展開

foo

b a r

-hoge

pi

動詞の完全な一覧

Get-Verb

Verb        AliasPrefix Group          Description
----        ----------- -----          -----------
Add         a           Common         Adds a resource to a container, or attaches an item to another item
Clear       cl          Common         Removes all the resources from a container but does not delete the container
Close       cs          Common         Changes the state of a resource to make it inaccessible, unavailable, or unusable
Copy        cp          Common         Copies a resource to another name or to another container
Enter       et          Common         Specifies an action that allows the user to move into a resource
Exit        ex          Common         Sets the current environment or context to the most recently used context
Find        fd          Common         Looks for an object in a container that is unknown, implied, optional, or specified
Format      f           Common         Arranges objects in a specified form or layout
Get         g           Common         Specifies an action that retrieves a resource
Hide        h           Common         Makes a resource undetectable
Join        j           Common         Combines resources into one resource
Lock        lk          Common         Secures a resource
Move        m           Common         Moves a resource from one location to another
New         n           Common         Creates a resource
Open        op          Common         Changes the state of a resource to make it accessible, available, or usable
Optimize    om          Common         Increases the effectiveness of a resource
Push        pu          Common         Adds an item to the top of a stack
Pop         pop         Common         Removes an item from the top of a stack
Redo        re          Common         Resets a resource to the state that was undone
Remove      r           Common         Deletes a resource from a container
Rename      rn          Common         Changes the name of a resource
Reset       rs          Common         Sets a resource back to its original state
Resize      rz          Common         Changes the size of a resource
Search      sr          Common         Creates a reference to a resource in a container
Select      sc          Common         Locates a resource in a container
Set         s           Common         Replaces data on an existing resource or creates a resource that contains some data
Show        sh          Common         Makes a resource visible to the user
Skip        sk          Common         Bypasses one or more resources or points in a sequence
Split       sl          Common         Separates parts of a resource
Step        st          Common         Moves to the next point or resource in a sequence
Switch      sw          Common         Specifies an action that alternates between two resources, such as to change between two locations, responsibilities, or states
Undo        un          Common         Sets a resource to its previous state
Unlock      uk          Common         Releases a resource that was locked
Watch       wc          Common         Continually inspects or monitors a resource for changes
Connect     cc          Communications Creates a link between a source and a destination
Disconnect  dc          Communications Breaks the link between a source and a destination
Read        rd          Communications Acquires information from a source
Receive     rc          Communications Accepts information sent from a source
Send        sd          Communications Delivers information to a destination
Write       wr          Communications Adds information to a target
Backup      ba          Data           Stores data by replicating it
Checkpoint  ch          Data           Creates a snapshot of the current state of the data or of its configuration
Compare     cr          Data           Evaluates the data from one resource against the data from another resource
Compress    cm          Data           Compacts the data of a resource
Convert     cv          Data           Changes the data from one representation to another when the cmdlet supports bidirectional conversion or when the cmdlet supports conversio…
ConvertFrom cf          Data           Converts one primary type of input (the cmdlet noun indicates the input) to one or more supported output types
ConvertTo   ct          Data           Converts from one or more types of input to a primary output type (the cmdlet noun indicates the output type)
Dismount    dm          Data           Detaches a named entity from a location
Edit        ed          Data           Modifies existing data by adding or removing content
Expand      en          Data           Restores the data of a resource that has been compressed to its original state
Export      ep          Data           Encapsulates the primary input into a persistent data store, such as a file, or into an interchange format
Group       gp          Data           Arranges or associates one or more resources
Import      ip          Data           Creates a resource from data that is stored in a persistent data store (such as a file) or in an interchange format
Initialize  in          Data           Prepares a resource for use, and sets it to a default state
Limit       l           Data           Applies constraints to a resource
Merge       mg          Data           Creates a single resource from multiple resources
Mount       mt          Data           Attaches a named entity to a location
Out         o           Data           Sends data out of the environment
Publish     pb          Data           Makes a resource available to others
Restore     rr          Data           Sets a resource to a predefined state, such as a state set by Checkpoint
Save        sv          Data           Preserves data to avoid loss
Sync        sy          Data           Assures that two or more resources are in the same state
Unpublish   ub          Data           Makes a resource unavailable to others
Update      ud          Data           Brings a resource up-to-date to maintain its state, accuracy, conformance, or compliance
Debug       db          Diagnostic     Examines a resource to diagnose operational problems
Measure     ms          Diagnostic     Identifies resources that are consumed by a specified operation, or retrieves statistics about a resource
Ping        pi          Diagnostic     Use the Test verb
Repair      rp          Diagnostic     Restores a resource to a usable condition
Resolve     rv          Diagnostic     Maps a shorthand representation of a resource to a more complete representation
Test        t           Diagnostic     Verifies the operation or consistency of a resource
Trace       tr          Diagnostic     Tracks the activities of a resource
Approve     ap          Lifecycle      Confirms or agrees to the status of a resource or process
Assert      as          Lifecycle      Affirms the state of a resource
Build       bd          Lifecycle      Creates an artifact (usually a binary or document) out of some set of input files (usually source code or declarative documents)
Complete    cmp         Lifecycle      Concludes an operation
Confirm     cn          Lifecycle      Acknowledges, verifies, or validates the state of a resource or process
Deny        dn          Lifecycle      Refuses, objects, blocks, or opposes the state of a resource or process
Deploy      dp          Lifecycle      Sends an application, website, or solution to a remote target[s] in such a way that a consumer of that solution can access it after deploym…
Disable     d           Lifecycle      Configures a resource to an unavailable or inactive state
Enable      e           Lifecycle      Configures a resource to an available or active state
Install     is          Lifecycle      Places a resource in a location, and optionally initializes it
Invoke      i           Lifecycle      Performs an action, such as running a command or a method
Register    rg          Lifecycle      Creates an entry for a resource in a repository such as a database
Request     rq          Lifecycle      Asks for a resource or asks for permissions
Restart     rt          Lifecycle      Stops an operation and then starts it again
Resume      ru          Lifecycle      Starts an operation that has been suspended
Start       sa          Lifecycle      Initiates an operation
Stop        sp          Lifecycle      Discontinues an activity
Submit      sb          Lifecycle      Presents a resource for approval
Suspend     ss          Lifecycle      Pauses an activity
Uninstall   us          Lifecycle      Removes a resource from an indicated location
Unregister  ur          Lifecycle      Removes the entry for a resource from a repository
Wait        w           Lifecycle      Pauses an operation until a specified event occurs
Use         u           Other          Uses or includes a resource to do something
Block       bl          Security       Restricts access to a resource
Grant       gr          Security       Allows access to a resource
Protect     pt          Security       Safeguards a resource from attack or loss
Revoke      rk          Security       Specifies an action that does not allow access to a resource
Unblock     ul          Security       Removes restrictions to a resource
Unprotect   up          Security       Removes safeguards from a resource that were added to prevent it from attack or loss

パイプライン

配列

# 0..4 | ForEach-Object {
0..4 | % {
    if ($_ -gt 2) {
        return # スクリプトブロック内なので、continueではなくreturn
    }
    $_
}

0

1

2

0..5 + 3..8 | Sort-Object | Get-Unique

0

1

2

3

4

5

6

7

8

0..4 | Select-Object -First 2
0..4 | Select-Object -Last 2

0

1

3

4

0..4 | Sort-Object

1

2

3

4

# 0..4 | Where-Object { $_ -lt 2 }
0..4 | ? { $_ -lt 2 }

0

1

表示

# すべてのプロパティ
Get-ChildItem | Select-Object *

Get-ChildItem | Format-List
Get-ChildItem | Format-List Name, Length, Mode

Get-ChildItem | Format-Table
Get-ChildItem | Format-Table Name, Length, Mode

Get-ChildItem | Format-Wide
Get-ChildItem | Format-Wide Name -Column 1

Tee

ファイルまたは変数にコマンド出力を保存しつつ標準出力

Get-ChildItem | Tee-Object -Variable fileList

$fileList
Get-ChildItem | Tee-Object -FilePath "fileList.txt" -Append

データ型

type FullName MinValue MaxValue
[array] System.Array
[bool] System.Boolean
[byte] System.Byte 0 255
[char] System.Char
[datetime] System.DateTime 0001/01/01 0:00:00 9999/12/31 23:59:59
[decimal] System.Decimal -79228162514264337593543950335 79228162514264337593543950335
[double] System.Double -1.79769313486232E+308 1.79769313486232E+308
[guid] System.Guid
[hashtable] System.Collections.Hashtable
[int16] System.Int16 -32768 32767
[int32], [int] System.Int32 -2147483648 2147483647
[int64], [long] System.Int64 -9223372036854775808 9223372036854775807
[nullable] System.Nullable
[psobject] System.Management.Automation.PSObject
[regex] System.Text.RegularExpressions.Regex
[sbyte] System.SByte -128 127
[scriptblock] System.Management.Automation.ScriptBlock
[single], [float] System.Single -3.402823E+38 3.402823E+38
[string] System.String
[switch] System.Management.Automation.SwitchParameter
[timespan] System.TimeSpan -10675199.02:48:05.4775808 10675199.02:48:05.4775807
[type] System.Type
[uint16] System.UInt16 0 65535
[uint32] System.UInt32 0 4294967295
[uint64] System.UInt64 0 18446744073709551615
[xml] System.Xml.XmlDocument
$type = [array]
# $type = $var.GetType()

Write-Output ("| {0} | {1} | {2} | {3} |" -f $type.Name, $type.FullName, $type::MinValue, $type::MaxValue)

リテラル

数値

123
0x10
1KB
10MB

123

16

1024

10485760

文字列

'Lorem ipsum'
"Lorem ipsum"

Lorem ipsum

Lorem ipsum

エスケープ

''''
# '`'' ←これだけNG

'"'
""""
"`""

'`'
"``"

"`$PSHOME"
変数展開

$ipsum  = "ipsum"

'Lorem $ipsum'
"Lorem $ipsum"

Lorem $ipsum

Lorem ipsum

式展開

"Lorem$('L' + 'o' * 3)ipsum"

LoremLoooipsum

ヒアドキュメント

$ipsum  = "ipsum"

@'
Lorem
$ipsum
'@

@"
Lorem
$ipsum
"@

Lorem

$ipsum

Lorem

ipsum

部分文字列

$lipsum = "Lorem ipsum"
$len=2


Write-Host $lipsum.Substring(0, $len)

Write-Host $lipsum.Substring($lipsum.Length - $len, $len)

Lo

um

セキュア文字列(Windows PowerShellのみ)

$sourceString = "sensitive text data"
Write-Host $sourceString

$secureString = ConvertTo-SecureString -string $sourceString -AsPlainText -Force
Write-Host $secureString

$SecureString | ConvertFrom-SecureString | Out-File .\password.txt



$SecureString = Get-Content .\password.txt | ConvertTo-SecureString
Write-Host $secureString

$plainString = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecureString))
write-host $plainString

sensitive text data

System.Security.SecureString

System.Security.SecureString

sensitive text data

演算子

算術演算子

演算子 比較内容
+
++ インクリメント
-
-- デクリメント
*
/
% 剰余

冪乗には関数Powを用いる

比較演算子

演算子 比較内容
-eq 等しい
-ne 等しくない
-gt より大きい
-ge 以上
-le 以下
-lt より小さい
-contains 含む ( "abc", "def" -contains "def" )
-notcontains 含まない
-in 含む ( "def" -in "abc", "def" )
-notin 含まない
-is 型が等しい
-isnot 型が等しくない
$a = 1
$b = "1"
$a -is [int]        # True
$a -is $b.GetType() # False

論理演算子

演算子 比較内容
-and 積(かつ)
-or 和(または)
-not 否定
-xor 排他的論理和

ビット演算子

演算子 比較内容
-band 積(かつ)
-bor 和(または)
-bnot 否定
-bxor 排他的論理和

文字列演算子

演算子 比較内容
+ 結合
* 繰り返し
-f フォーマット
-like ワイルドカード(*, ?)による文字列比較
-notlike 否定形
-match 正規表現による文字列比較
-notmatch 否定形
-replace -ireplace 正規表現を使用して置換(大小文字を区別しない; Case insensitive)
-creplace 正規表現を使用して置換(大小文字を区別する; Case-sensitive)

※ -replace 演算子は、Replace()メソッドとは異なり正規表現で置換する

"PowerShell" -like    "*shell"           # True
"PowerShell" -like    "Power?hell"       # True
"PowerShell" -like    "Power[p-w]hell"   # True
"PowerShell", "Server" -like "*shell"    # PowerShell
"PowerShell", "Server" -notlike "*shell" # Server

"PowerShell" -match 'shell'              # True
"PowerShell" -like  'shell'              # False
"PowerShell" -match    '^Power\w+'       # True
'bag'        -notmatch 'b[iou]g'         # True

"Bag", "Beg", "Big", "Bog", "Bug"  -match 'b[iou]g'    # Big, Bog, Bug
"Bag", "Beg", "Big", "Bog", "Bug"  -notmatch 'b[iou]g' # Bag, Beg

'5.72' -replace '(.+)', '$ $1' # $ 5.72
'5.72' -replace '(.+)', '$$$1' # $5.72
'5.72' -replace '(.+)', '$$1'  # $1

"B1","B2","B3","B4","B5" -replace "B", 'a'
# a1
# a2
# a3
# a4
# a5
"{0}"       -f 1.41421356, 1.7320508 # 1.41421356
"{0} {1}"   -f 1.41421356, 1.7320508 # 1.41421356 1.7320508

"|{0,20}|"  -f 1.41421356 # |          1.41421356|
"|{0,-20}|" -f 1.41421356 # |1.41421356          |

"{0:0.0000000}"     -f 1.41421356 # 1.4142136 (四捨五入)
"{0:00.0000000000}" -f 1.41421356 # 01.4142135600

"{0:#.#######}"     -f 1.41421356 # 1.4142136 (四捨五入)
"{0:##.##########}" -f 1.41421356 # 1.41421356

"{0:N}"  -f 1234.567 # 1,234.57
"{0:N0}" -f 1234.567 # 1,235
"{0:N1}" -f 1234.567 # 1,234.6

変数

$a = 1
$b = $c = $a

New-Variable -name a

型を指定

$a = 1 # 型を指定しなければObject型

# 明示的に型を指定
[byte]$val = 1
[double]$val = 0
[int]$val = 3
[string]$val = "4"

スコープ

スコープ 内容
private ブロック内
local ブロックと子ブロック
script ファイル内
global ファイル外

自動変数・設定変数

Get-Variable
Get-Variable | Get-Member -MemberType Properties
変数名 内容
$env:ENVVAR 環境変数
$_ ( PSItem と同じ) パイプラインオブジェクトの現在のオブジェクト
$? 最後のコマンドの実行状態(リターンコード)
$^ 1つ前のコマンド履歴
$$ 1つ前のコマンド履歴
$args コマンドライン引数
$cert
$ConfirmPreference
$DebugPreference
$EnabledExperimentalFeatures
$Error 最後のエラー
$ErrorActionPreference
$ErrorView
$ExecutionContext
$false False
$FormatEnumerationLimit
$HOME ホームディレクトリのパス
$Host ホストアプリケーションを表すオブジェクト
$InformationPreference
$input 関数に渡される入力
$IsCoreCLR
$IsLinux
$IsMacOS
$IsWindows
$MaximumHistoryCount
$MyInvocation 現在のコマンドに関する情報
$NestedPromptLevel
$null NULL
$OutputEncoding
$PID ホストしているプロセスのプロセス識別子 (PID)
$profile PowerShell プロファイルのパス
$ProgressPreference
$PSBoundParameters
$PSCommandPath
$PSCulture
$PSDefaultParameterValues
$PSEdition
$psEditor
$PSEmailServer
$PSHOME PowerShell のインストールディレクトリのパス
$PSScriptRoot スクリプトの実行元のディレクトリ
$PSSessionApplicationName
$PSSessionConfigurationName
$PSSessionOption
$PSStyle
$PSUICulture
$PSVersionTable PowerShell のバージョン
$PWD カレントディレクトリ
$ShellId
$StackTrace
$targetScriptPath
$true True
$VerbosePreference
$WarningPreference
$WhatIfPreference

設定変数のみ一覧化

Get-Variable |
  Where-Object {
    $_.Name -clike '*Preference' -or
    ($_.Attributes -and $_.Options -notcontains 'ReadOnly')
  } | % Name

配列

# 配列の宣言
$array1 = @()
[int[]]$array2

# 配列への代入
$array3 = @(1)
$array3 = ,1

$array4 = @(2,3,4)
$array4 = @(0..4)
$array4 = 2,3,4
$array4 = 0..4

# 配列の結合
$array = $array1 + $array2

# 要素の追加と変更
$array5 = @()
$array5[0] = 555 # これはSystem.IndexOutOfRangeExceptionとなる
$array5 += 555 # 末尾に追加
$array5 += 5555
$array5[0] = 55

# 要素の削除
$array6 = @(6,66,666)
$array6
$array6[1] = $null
$array6

6

66

666

6

666

$array = 0..1000

# 指定された値が配列に含まれるか検査する
$array -contains 5

# 配列の要素を参照
$array[0]

$array[-1]

$array[1,3,5]

$array[10..200] # 添え字も配列

True

0

1000

1

3

5

10

11

(中略)

200

多次元配列

$array = @(("1-1","1-2","1-3","1-4"),("2-1","2-2","2-3","2-4"))
$array[1][2]

2-3

オブジェクトの配列

$AppUsers = @(
    [pscustomobject] @{FirstName="Owen"; LastName="Hardacre"; Mail="[email protected]"},
    [pscustomobject] @{FirstName="Luke"; LastName="Edmunds"; Mail="[email protected]"},
    [pscustomobject] @{FirstName="John"; LastName="Graham"; Mail="[email protected]"},
    [pscustomobject] @{FirstName="Elizabeth"; LastName="Dyer"; Mail="[email protected]"},
    [pscustomobject] @{FirstName="Robert"; LastName="Wright"; Mail="[email protected]"}
)

# 個々の要素にアクセス
$AppUsers[0]

# プロパティを参照
$AppUsers[0].FirstName

# プロパティを更新
$AppUsers[0].FirstName = "Oliver"
$AppUsers[0].FirstName

# 全ての要素のプロパティ
$AppUsers | ForEach-Object {$_.Mail}
$AppUsers | Select-Object -ExpandProperty Mail
$AppUsers.Mail

# 要素のフィルタリング
$AppUsers | Where-Object {$_.FirstName -eq "Luke"} | Select-Object -ExpandProperty Mail
$AppUsers.Where({$_.FirstName -eq "Luke"}).Mail

# オブジェクトの配列をソート
$AppUsers | Where-Object {$_.FirstName -ne $null} | Sort-Object "LastName" -Descending | Select-Object -First 3

FirstName LastName Mail


Owen Hardacre [email protected]

Owen

Oliver

[email protected]

[email protected]

[email protected]

[email protected]

[email protected]

[email protected]

FirstName LastName Mail


Robert Wright [email protected]

Oliver Hardacre [email protected]

John Graham [email protected]

ArrayList

$list = New-Object -TypeName 'System.Collections.ArrayList';
$list.Add("lorem")
$list.Add("ipsum")
$list.Add(123)
$list.Add("dolor")
$list.Add("sit")
$list.Add(456)
$list.Add("amet")
$list.RemoveAt(0)
$list.RemoveRange(1,4)
$list.Count

$list | sort-object

amet

ipsum

ハッシュテーブル

# 連想配列の宣言と代入
$table = @{key1 = "value1"; key2 = "value2"; key3 = "value3"}

# 順序付けられたディクショナリ
$table = [ordered]@{key1 = "value1"; key2 = "value2"; key3 = "value3"}

$table["key1"]

value1

# 要素の追加
$table.Add("key4", "value4")
# OR
$table.key4 = "value4"

# 要素の変更
$table["key3"] = "value33"
# OR
$table.key3 = "value33"

# 要素を削除
$table.Remove("key2")

Name Value


key3 value33 key1 value1 key4 value4

# 指定されたキーに対応する値が存在するか検査する
$table.ContainsKey("key1")

# 指定された値に対応するキーが存在するか検査する
$table.ContainsValue("value33")

# 指定されたキーが配列に含まれるか検査する
$table.Contains("key1")
$table.Contains("value0")

True False

# すべてのキーを取得する
$keys = $table.Keys

# すべての値を取得する
$values = $table.Values

# 要素ごとに処理する
foreach ($key in $table.Keys) {
    $key + ": " + $table[$key]
}

$table.GetEnumerator() | ForEach-Object {
  $key = $_.Key
  $value = $_.Value

  $key + ": " + $value
}

key3: value3 key1: value1 key2: value2

画面出力

"画面出力 $a" # Write-Outputと同じ
'画面出力 $a'

Write-Host "画面出力" # コンソールに出力
Write-Output "画面出力" # パイプラインに渡す(パイプラインがなければコンソールに出力される)

Write-Host "画面出力" -NoNewline # 文末で改行しない
Write-Host "画面出力" -ForegroundColor Blue

$Host.UI.WriteLine() # コンソールに出力
$Host.UI.WriteWarningLine()

[System.Console]::Write()
[System.Console]::WriteLine()

@"
画面
出力
"
@'
画面
出力
'

デバッグ出力

Write-Error

# $ErrorActionPreference変数の値によってメッセージ表示/非表示、処理の継続/停止を指定する
try {
    $savedErrorActionPreference = $ErrorActionPreference  # 現在の設定値を保存する
    $ErrorActionPreference = "Stop"
    Write-Error -Message "画面出力" # エラー出力を行い、catch節に飛ばす
} catch {
    # 例外処理
} finally {
    $ErrorActionPreference = $savedErrorActionPreference  #既定値に戻す
}

その他のログレベル

$DebugPreference
Write-Debug -Message "画面出力"

Write-Debug -Message "画面出力" -Debug # $DebugPreferenceの値が"SilentryContinue"であっても出力する

$VerbosePreference
Write-Verbose -Message "画面出力"

$WarningPreference
Write-Warning -Message "画面出力"
# Write-Hostの、文字色のみログレベル別に変化させたもの
$Host.UI.WriteDebugLine("画面出力")
$Host.UI.WriteErrorLine("画面出力")
$Host.UI.WriteVerboseLine("画面出力")
$Host.UI.WriteWarningLine("画面出力")

プログレスバー

$activity = "読込処理"
$currentOperation = "データを読込んでいます。"
for($i = 0;$i -le 100; $i++){
   $status = "{0} %" -F $i
   Write-Progress $activity $status -PercentComplete $i -CurrentOperation $currentOperation

  #
  # 読込処理
  #

}

プロセス

管理者権限で実行

$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$isAdmin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

if (!$isAdmin) {
    Start-Process pwsh "-File `"$PSCommandPath`"" -Verb RunAs
    exit
}
else {
    # 管理者権限で実行する処理
}

待機

キー押下を待機(Pause)

function Pause {
    if ($psISE) {
        $null = Read-Host 'Press Enter Key...'
    }
    else {
        Write-Host "Press Any Key..."
        (Get-Host).UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | Out-Null
    }
}

一定時間待機(Sleep, Wait)

# 10s
Start-Sleep -s 10; Write-Host "###"

#3000ms
Start-Sleep -m 3000; Write-Host "###"

制御構文

条件分岐

if文

if ($cond1) {
    echo "true1"
} elseif ($cond2) {
    echo "true2"
} else {
     echo "false"
}

TRUEと評価される値

$true
1
'a'
"a"

FALSEと評価される値

$false
0
""

switch文

$v = 1
switch ($v) {
    1 { Write-Host "one" }
    2 { Write-Host "two" }
    Default { Write-Host "default" }
}

ループ

ループ文

for ($i = 0; $i -lt 4; $i++) {
    echo $i
}
foreach ($i in @(1, 2, 3, 4)) {
    echo $i
}

foreach ($i in 0..4) {
    echo $i
}

$map = @{k1="v1"; k2="v2"; k3="v3"}
foreach ($i in $map) {
    echo $i
}

$list = New-Object System.Collections.ArrayList
$list.Add("i1")
$list.Add("i2") > $null
[void]$list.Add("i3")
foreach ($i in $list) {
    echo $i
}
$i = 0
while ($i -lt 4) {
    echo $i
    $i++
}
$i = 0
do {
    echo $i
    $i++
} while ($i -lt 4)
$i = 0
do {
    echo $i
    $i++
} until ($i -gt 4-1)

いずれも

0 1 2 3

ループ制御文

foreach ($i in 0..4) {
    if($i -eq 3){
        break
    }
    echo $i
}

1 2

foreach ($i in 0..4) {
    if($i -eq 3){
        continue
    }
    echo $i
}

1 2 4

ForEach メソッド

$data = 1..3
$data.foreach({"Data [$PSItem]"})

Data [1]

Data [2]

Data [3]

パイプライン

$data = 1..3
$data | ForEach-Object {"Data [$PSItem]"}

例外処理

try節

PowerShellのエラー「終了するエラー」「続行するエラー」のうち、デフォルトでCatchできるのは「終了するエラー」のみだが、$ErrorActionPreferenceに「Stop」を代入することで、両方例外としてCatchすることができるようになる。

定数 内容
continue 例外は出力しつつ、処理も継続する(既定値)
silentlycontinue 例外を出力せずに、処理を継続する
stop 処理を終了する
$ErrorActionPreference = "Stop"

# 戻り値
$rc = 0

try{
    Write-Host "処理1を実行します。"
    Set-Content -Path ".\not_exist\not_exist.txt" -Value "test"
    Write-Host "処理2を実行します。"
}catch{
# }catch [Exception]{
    Write-Host "catch例外処理です。"
    Write-Host "Error:" $Error
    Write-Host "StackTrace:" $StackTrace
    $rc = 1
}finally{
    Write-Host "finally処理です。"
}

exit $rc

trap節

# 例外発生時にスクリプトを停止する
trap [Exception] {
    write-host("ps_exception_2():trap")
    break
}

# 処理
# 例外発生時もスクリプトを継続する
trap [Exception] {
    write-host("ps_exception_1():trap")
    continue
  }

# 処理

関数

関数の基本形

function Func ($arg){
    return "戻り値"
}

# 呼出しに括弧を使わない
$result = Func $arg1 $arg2 $arg3

引数

引数の取り方

function Func1($arg1, $arg2){ # 引数リストの使用

    # 処理

    return "戻り値"
}

function Func2 {
    param($arg1, $arg2) # paramキーワードを使用

    # 処理

    return "戻り値"
}

function Func3 {
    $arg1 = $args[0] # 自動変数$argsを使用
    $arg2 = $args[1]

    # 処理

    return "戻り値"
}

引数の型を指定

function Func4([int]$arg1, [int]$arg2){
    return $arg1 + $arg2
}

引数の参照渡し

function Func5([ref]$arg1, [ref]$arg2){
    return "戻り値"
}

引数の既定値

function Func5($arg1 = 1, $arg2 = 2){
    return "戻り値"
}

数値計算

四捨五入

小数

$numVal = [Math]::Round(1.4, [MidpointRounding]::AwayFromZero)
$numVal # 1
$numVal = [Math]::Round(1.5, [MidpointRounding]::AwayFromZero)
$numVal # 2

$numVal = [Math]::Round(-1.4, [MidpointRounding]::AwayFromZero)
$numVal # -1
$numVal = [Math]::Round(-1.5, [MidpointRounding]::AwayFromZero)
$numVal # -2

# 小数点以下第一位まで残す
$numVal = [Math]::Round(0.04, 1, [MidpointRounding]::AwayFromZero)
$numVal # 0
$numVal = [Math]::Round(0.05, 1, [MidpointRounding]::AwayFromZero)
$numVal # 0.1

整数

function Round([double]$num, [int]$numDigits)
{
    $m = [Math]::Pow(10, -1 * $numDigits)
    return [Math]::Round($num * $m, [MidpointRounding]::AwayFromZero) / $m
}

$numVal = Round 1234 2
$numVal # 1200

切り捨て

$numVal = [Math]::Truncate(1.5)
$numVal # 1
$numVal = [Math]::Floor(1.5)
$numVal # 1

function Truncate([double]$num, [int]$numDigits = 0)
{
    $m = [Math]::Pow(10, $numDigits)
    return [Math]::Truncate($num * $m) / $m
}
$numVal = Truncate 1.234 2
$numVal # 1.23
$numVal = Truncate -1.234 2
$numVal # -1.23

システム情報

Get-ComputerInfo
Get-ComputerInfo | Get-Member -MemberType Properties
Name                                                    MemberType Definition
----                                                    ---------- ----------
BiosBIOSVersion                                         Property   string[] BiosBIOSVersion {get;}
BiosBuildNumber                                         Property   string BiosBuildNumber {get;}
BiosCaption                                             Property   string BiosCaption {get;}
BiosCharacteristics                                     Property   ushort[] BiosCharacteristics {get;}
BiosCodeSet                                             Property   string BiosCodeSet {get;}
BiosCurrentLanguage                                     Property   string BiosCurrentLanguage {get;}
BiosDescription                                         Property   string BiosDescription {get;}
BiosEmbeddedControllerMajorVersion                      Property   System.Nullable[short] BiosEmbeddedControllerMajorVersion {get;}
BiosEmbeddedControllerMinorVersion                      Property   System.Nullable[short] BiosEmbeddedControllerMinorVersion {get;}
BiosFirmwareType                                        Property   System.Nullable[Microsoft.PowerShell.Commands.FirmwareType] BiosFirmwareType {get;}
BiosIdentificationCode                                  Property   string BiosIdentificationCode {get;}
BiosInstallableLanguages                                Property   System.Nullable[ushort] BiosInstallableLanguages {get;}
BiosInstallDate                                         Property   System.Nullable[datetime] BiosInstallDate {get;}
BiosLanguageEdition                                     Property   string BiosLanguageEdition {get;}
BiosListOfLanguages                                     Property   string[] BiosListOfLanguages {get;}
BiosManufacturer                                        Property   string BiosManufacturer {get;}
BiosName                                                Property   string BiosName {get;}
BiosOtherTargetOS                                       Property   string BiosOtherTargetOS {get;}
BiosPrimaryBIOS                                         Property   System.Nullable[bool] BiosPrimaryBIOS {get;}
BiosReleaseDate                                         Property   System.Nullable[datetime] BiosReleaseDate {get;}
BiosSerialNumber                                        Property   string BiosSerialNumber {get;}
BiosSMBIOSBIOSVersion                                   Property   string BiosSMBIOSBIOSVersion {get;}
BiosSMBIOSMajorVersion                                  Property   System.Nullable[ushort] BiosSMBIOSMajorVersion {get;}
BiosSMBIOSMinorVersion                                  Property   System.Nullable[ushort] BiosSMBIOSMinorVersion {get;}
BiosSMBIOSPresent                                       Property   System.Nullable[bool] BiosSMBIOSPresent {get;}
BiosSoftwareElementState                                Property   System.Nullable[Microsoft.PowerShell.Commands.SoftwareElementState] BiosSoftwareElementState {get;}
BiosStatus                                              Property   string BiosStatus {get;}
BiosSystemBiosMajorVersion                              Property   System.Nullable[ushort] BiosSystemBiosMajorVersion {get;}
BiosSystemBiosMinorVersion                              Property   System.Nullable[ushort] BiosSystemBiosMinorVersion {get;}
BiosTargetOperatingSystem                               Property   System.Nullable[ushort] BiosTargetOperatingSystem {get;}
BiosVersion                                             Property   string BiosVersion {get;}
CsAdminPasswordStatus                                   Property   System.Nullable[Microsoft.PowerShell.Commands.HardwareSecurity] CsAdminPasswordStatus {get;}
CsAutomaticManagedPagefile                              Property   System.Nullable[bool] CsAutomaticManagedPagefile {get;}
CsAutomaticResetBootOption                              Property   System.Nullable[bool] CsAutomaticResetBootOption {get;}
CsAutomaticResetCapability                              Property   System.Nullable[bool] CsAutomaticResetCapability {get;}
CsBootOptionOnLimit                                     Property   System.Nullable[Microsoft.PowerShell.Commands.BootOptionAction] CsBootOptionOnLimit {get;}
CsBootOptionOnWatchDog                                  Property   System.Nullable[Microsoft.PowerShell.Commands.BootOptionAction] CsBootOptionOnWatchDog {get;}
CsBootROMSupported                                      Property   System.Nullable[bool] CsBootROMSupported {get;}
CsBootStatus                                            Property   ushort[] CsBootStatus {get;}
CsBootupState                                           Property   string CsBootupState {get;}
CsCaption                                               Property   string CsCaption {get;}
CsChassisBootupState                                    Property   System.Nullable[Microsoft.PowerShell.Commands.SystemElementState] CsChassisBootupState {get;}
CsChassisSKUNumber                                      Property   string CsChassisSKUNumber {get;}
CsCurrentTimeZone                                       Property   System.Nullable[short] CsCurrentTimeZone {get;}
CsDaylightInEffect                                      Property   System.Nullable[bool] CsDaylightInEffect {get;}
CsDescription                                           Property   string CsDescription {get;}
CsDNSHostName                                           Property   string CsDNSHostName {get;}
CsDomain                                                Property   string CsDomain {get;}
CsDomainRole                                            Property   System.Nullable[Microsoft.PowerShell.Commands.DomainRole] CsDomainRole {get;}
CsEnableDaylightSavingsTime                             Property   System.Nullable[bool] CsEnableDaylightSavingsTime {get;}
CsFrontPanelResetStatus                                 Property   System.Nullable[Microsoft.PowerShell.Commands.HardwareSecurity] CsFrontPanelResetStatus {get;}
CsHypervisorPresent                                     Property   System.Nullable[bool] CsHypervisorPresent {get;}
CsInfraredSupported                                     Property   System.Nullable[bool] CsInfraredSupported {get;}
CsInitialLoadInfo                                       Property   string CsInitialLoadInfo {get;}
CsInstallDate                                           Property   System.Nullable[datetime] CsInstallDate {get;}
CsKeyboardPasswordStatus                                Property   System.Nullable[Microsoft.PowerShell.Commands.HardwareSecurity] CsKeyboardPasswordStatus {get;}
CsLastLoadInfo                                          Property   string CsLastLoadInfo {get;}
CsManufacturer                                          Property   string CsManufacturer {get;}
CsModel                                                 Property   string CsModel {get;}
CsName                                                  Property   string CsName {get;}
CsNetworkAdapters                                       Property   Microsoft.PowerShell.Commands.NetworkAdapter[] CsNetworkAdapters {get;}
CsNetworkServerModeEnabled                              Property   System.Nullable[bool] CsNetworkServerModeEnabled {get;}
CsNumberOfLogicalProcessors                             Property   System.Nullable[uint] CsNumberOfLogicalProcessors {get;}
CsNumberOfProcessors                                    Property   System.Nullable[uint] CsNumberOfProcessors {get;}
CsOEMStringArray                                        Property   string[] CsOEMStringArray {get;}
CsPartOfDomain                                          Property   System.Nullable[bool] CsPartOfDomain {get;}
CsPauseAfterReset                                       Property   System.Nullable[long] CsPauseAfterReset {get;}
CsPCSystemType                                          Property   System.Nullable[Microsoft.PowerShell.Commands.PCSystemType] CsPCSystemType {get;}
CsPCSystemTypeEx                                        Property   System.Nullable[Microsoft.PowerShell.Commands.PCSystemTypeEx] CsPCSystemTypeEx {get;}
CsPhysicallyInstalledMemory                             Property   System.Nullable[ulong] CsPhysicallyInstalledMemory {get;}
CsPowerManagementCapabilities                           Property   Microsoft.PowerShell.Commands.PowerManagementCapabilities[] CsPowerManagementCapabilities {get;}
CsPowerManagementSupported                              Property   System.Nullable[bool] CsPowerManagementSupported {get;}
CsPowerOnPasswordStatus                                 Property   System.Nullable[Microsoft.PowerShell.Commands.HardwareSecurity] CsPowerOnPasswordStatus {get;}
CsPowerState                                            Property   System.Nullable[Microsoft.PowerShell.Commands.PowerState] CsPowerState {get;}
CsPowerSupplyState                                      Property   System.Nullable[Microsoft.PowerShell.Commands.SystemElementState] CsPowerSupplyState {get;}
CsPrimaryOwnerContact                                   Property   string CsPrimaryOwnerContact {get;}
CsPrimaryOwnerName                                      Property   string CsPrimaryOwnerName {get;}
CsProcessors                                            Property   Microsoft.PowerShell.Commands.Processor[] CsProcessors {get;}
CsResetCapability                                       Property   System.Nullable[Microsoft.PowerShell.Commands.ResetCapability] CsResetCapability {get;}
CsResetCount                                            Property   System.Nullable[short] CsResetCount {get;}
CsResetLimit                                            Property   System.Nullable[short] CsResetLimit {get;}
CsRoles                                                 Property   string[] CsRoles {get;}
CsStatus                                                Property   string CsStatus {get;}
CsSupportContactDescription                             Property   string[] CsSupportContactDescription {get;}
CsSystemFamily                                          Property   string CsSystemFamily {get;}
CsSystemSKUNumber                                       Property   string CsSystemSKUNumber {get;}
CsSystemType                                            Property   string CsSystemType {get;}
CsThermalState                                          Property   System.Nullable[Microsoft.PowerShell.Commands.SystemElementState] CsThermalState {get;}
CsTotalPhysicalMemory                                   Property   System.Nullable[ulong] CsTotalPhysicalMemory {get;}
CsUserName                                              Property   string CsUserName {get;}
CsWakeUpType                                            Property   System.Nullable[Microsoft.PowerShell.Commands.WakeUpType] CsWakeUpType {get;}
CsWorkgroup                                             Property   string CsWorkgroup {get;}
DeviceGuardAvailableSecurityProperties                  Property   Microsoft.PowerShell.Commands.DeviceGuardHardwareSecure[] DeviceGuardAvailableSecurityProperties {get;}
DeviceGuardCodeIntegrityPolicyEnforcementStatus         Property   System.Nullable[Microsoft.PowerShell.Commands.DeviceGuardConfigCodeIntegrityStatus] DeviceGuardCodeIntegrityPolicyEnforce…
DeviceGuardRequiredSecurityProperties                   Property   Microsoft.PowerShell.Commands.DeviceGuardHardwareSecure[] DeviceGuardRequiredSecurityProperties {get;}
DeviceGuardSecurityServicesConfigured                   Property   Microsoft.PowerShell.Commands.DeviceGuardSoftwareSecure[] DeviceGuardSecurityServicesConfigured {get;}
DeviceGuardSecurityServicesRunning                      Property   Microsoft.PowerShell.Commands.DeviceGuardSoftwareSecure[] DeviceGuardSecurityServicesRunning {get;}
DeviceGuardSmartStatus                                  Property   System.Nullable[Microsoft.PowerShell.Commands.DeviceGuardSmartStatus] DeviceGuardSmartStatus {get;}
DeviceGuardUserModeCodeIntegrityPolicyEnforcementStatus Property   System.Nullable[Microsoft.PowerShell.Commands.DeviceGuardConfigCodeIntegrityStatus] DeviceGuardUserModeCodeIntegrityPolic…
HyperVisorPresent                                       Property   System.Nullable[bool] HyperVisorPresent {get;}
HyperVRequirementDataExecutionPreventionAvailable       Property   System.Nullable[bool] HyperVRequirementDataExecutionPreventionAvailable {get;}
HyperVRequirementSecondLevelAddressTranslation          Property   System.Nullable[bool] HyperVRequirementSecondLevelAddressTranslation {get;}
HyperVRequirementVirtualizationFirmwareEnabled          Property   System.Nullable[bool] HyperVRequirementVirtualizationFirmwareEnabled {get;}
HyperVRequirementVMMonitorModeExtensions                Property   System.Nullable[bool] HyperVRequirementVMMonitorModeExtensions {get;}
KeyboardLayout                                          Property   string KeyboardLayout {get;}
LogonServer                                             Property   string LogonServer {get;}
OsArchitecture                                          Property   string OsArchitecture {get;}
OsBootDevice                                            Property   string OsBootDevice {get;}
OsBuildNumber                                           Property   string OsBuildNumber {get;}
OsBuildType                                             Property   string OsBuildType {get;}
OsCodeSet                                               Property   string OsCodeSet {get;}
OsCountryCode                                           Property   string OsCountryCode {get;}
OsCSDVersion                                            Property   string OsCSDVersion {get;}
OsCurrentTimeZone                                       Property   System.Nullable[short] OsCurrentTimeZone {get;}
OsDataExecutionPrevention32BitApplications              Property   System.Nullable[bool] OsDataExecutionPrevention32BitApplications {get;}
OsDataExecutionPreventionAvailable                      Property   System.Nullable[bool] OsDataExecutionPreventionAvailable {get;}
OsDataExecutionPreventionDrivers                        Property   System.Nullable[bool] OsDataExecutionPreventionDrivers {get;}
OsDataExecutionPreventionSupportPolicy                  Property   System.Nullable[Microsoft.PowerShell.Commands.DataExecutionPreventionSupportPolicy] OsDataExecutionPreventionSupportPolic…
OsDebug                                                 Property   System.Nullable[bool] OsDebug {get;}
OsDistributed                                           Property   System.Nullable[bool] OsDistributed {get;}
OsEncryptionLevel                                       Property   System.Nullable[Microsoft.PowerShell.Commands.OSEncryptionLevel] OsEncryptionLevel {get;}
OsForegroundApplicationBoost                            Property   System.Nullable[Microsoft.PowerShell.Commands.ForegroundApplicationBoost] OsForegroundApplicationBoost {get;}
OsFreePhysicalMemory                                    Property   System.Nullable[ulong] OsFreePhysicalMemory {get;}
OsFreeSpaceInPagingFiles                                Property   System.Nullable[ulong] OsFreeSpaceInPagingFiles {get;}
OsFreeVirtualMemory                                     Property   System.Nullable[ulong] OsFreeVirtualMemory {get;}
OsHardwareAbstractionLayer                              Property   string OsHardwareAbstractionLayer {get;}
OsHotFixes                                              Property   Microsoft.PowerShell.Commands.HotFix[] OsHotFixes {get;}
OsInstallDate                                           Property   System.Nullable[datetime] OsInstallDate {get;}
OsInUseVirtualMemory                                    Property   System.Nullable[ulong] OsInUseVirtualMemory {get;}
OsLanguage                                              Property   string OsLanguage {get;}
OsLastBootUpTime                                        Property   System.Nullable[datetime] OsLastBootUpTime {get;}
OsLocalDateTime                                         Property   System.Nullable[datetime] OsLocalDateTime {get;}
OsLocale                                                Property   string OsLocale {get;}
OsLocaleID                                              Property   string OsLocaleID {get;}
OsManufacturer                                          Property   string OsManufacturer {get;}
OsMaxNumberOfProcesses                                  Property   System.Nullable[uint] OsMaxNumberOfProcesses {get;}
OsMaxProcessMemorySize                                  Property   System.Nullable[ulong] OsMaxProcessMemorySize {get;}
OsMuiLanguages                                          Property   string[] OsMuiLanguages {get;}
OsName                                                  Property   string OsName {get;}
OsNumberOfLicensedUsers                                 Property   System.Nullable[uint] OsNumberOfLicensedUsers {get;}
OsNumberOfProcesses                                     Property   System.Nullable[uint] OsNumberOfProcesses {get;}
OsNumberOfUsers                                         Property   System.Nullable[uint] OsNumberOfUsers {get;}
OsOperatingSystemSKU                                    Property   System.Nullable[Microsoft.PowerShell.Commands.OperatingSystemSKU] OsOperatingSystemSKU {get;}
OsOrganization                                          Property   string OsOrganization {get;}
OsOtherTypeDescription                                  Property   string OsOtherTypeDescription {get;}
OsPAEEnabled                                            Property   System.Nullable[bool] OsPAEEnabled {get;}
OsPagingFiles                                           Property   string[] OsPagingFiles {get;}
OsPortableOperatingSystem                               Property   System.Nullable[bool] OsPortableOperatingSystem {get;}
OsPrimary                                               Property   System.Nullable[bool] OsPrimary {get;}
OsProductSuites                                         Property   Microsoft.PowerShell.Commands.OSProductSuite[] OsProductSuites {get;}
OsProductType                                           Property   System.Nullable[Microsoft.PowerShell.Commands.ProductType] OsProductType {get;}
OsRegisteredUser                                        Property   string OsRegisteredUser {get;}
OsSerialNumber                                          Property   string OsSerialNumber {get;}
OsServerLevel                                           Property   System.Nullable[Microsoft.PowerShell.Commands.ServerLevel] OsServerLevel {get;}
OsServicePackMajorVersion                               Property   System.Nullable[ushort] OsServicePackMajorVersion {get;}
OsServicePackMinorVersion                               Property   System.Nullable[ushort] OsServicePackMinorVersion {get;}
OsSizeStoredInPagingFiles                               Property   System.Nullable[ulong] OsSizeStoredInPagingFiles {get;}
OsStatus                                                Property   string OsStatus {get;}
OsSuites                                                Property   Microsoft.PowerShell.Commands.OSProductSuite[] OsSuites {get;}
OsSystemDevice                                          Property   string OsSystemDevice {get;}
OsSystemDirectory                                       Property   string OsSystemDirectory {get;}
OsSystemDrive                                           Property   string OsSystemDrive {get;}
OsTotalSwapSpaceSize                                    Property   System.Nullable[ulong] OsTotalSwapSpaceSize {get;}
OsTotalVirtualMemorySize                                Property   System.Nullable[ulong] OsTotalVirtualMemorySize {get;}
OsTotalVisibleMemorySize                                Property   System.Nullable[ulong] OsTotalVisibleMemorySize {get;}
OsType                                                  Property   System.Nullable[Microsoft.PowerShell.Commands.OSType] OsType {get;}
OsUptime                                                Property   System.Nullable[timespan] OsUptime {get;}
OsVersion                                               Property   string OsVersion {get;}
OsWindowsDirectory                                      Property   string OsWindowsDirectory {get;}
PowerPlatformRole                                       Property   System.Nullable[Microsoft.PowerShell.Commands.PowerPlatformRole] PowerPlatformRole {get;}
TimeZone                                                Property   string TimeZone {get;}
WindowsBuildLabEx                                       Property   string WindowsBuildLabEx {get;}
WindowsCurrentVersion                                   Property   string WindowsCurrentVersion {get;}
WindowsEditionId                                        Property   string WindowsEditionId {get;}
WindowsInstallationType                                 Property   string WindowsInstallationType {get;}
WindowsInstallDateFromRegistry                          Property   System.Nullable[datetime] WindowsInstallDateFromRegistry {get;}
WindowsProductId                                        Property   string WindowsProductId {get;}
WindowsProductName                                      Property   string WindowsProductName {get;}
WindowsRegisteredOrganization                           Property   string WindowsRegisteredOrganization {get;}
WindowsRegisteredOwner                                  Property   string WindowsRegisteredOwner {get;}
WindowsSystemRoot                                       Property   string WindowsSystemRoot {get;}
WindowsUBR                                              Property   System.Nullable[int] WindowsUBR {get;}
WindowsVersion                                          Property   string WindowsVersion {get;}

Windows Update

$windowsUpdate = New-Object -ComObject "Microsoft.Update.AutoUpdate"
$windowsUpdate.Settings
$windowsUpdate.Results
NotificationLevel         : 4
ReadOnly                  : False
Required                  : True
ScheduledInstallationDay  : 0
ScheduledInstallationTime : 3
IncludeRecommendedUpdates : True
NonAdministratorsElevated : True
FeaturedUpdatesEnabled    : False

LastSearchSuccessDate LastInstallationSuccessDate
--------------------- ---------------------------
yyyy/mm/dd h:mm:ss    yyyy/mm/dd h:mm:ss

Windows Firewall

$fw = new-object -com HNetCfg.FwMgr
$profile = $fw.LocalPolicy.CurrentProfile
$profile
Type                                         : 1
FirewallEnabled                              : True
ExceptionsNotAllowed                         : False
NotificationsDisabled                        : False
UnicastResponsesToMulticastBroadcastDisabled : False
RemoteAdminSettings                          : System.__ComObject
IcmpSettings                                 : System.__ComObject
GloballyOpenPorts                            : System.__ComObject
Services                                     : System.__ComObject
AuthorizedApplications                       : System.__ComObject

プロセス

Get-Process |
Where-Object {(($_.ProcessName -like "*firefox*") -or ($_.ProcessName -like "*chrome*"))} |
Select-Object -Property ProcessName,CPU |
Group-Object ProcessName |
Select-Object @{n='ProcessName';e={$_.Group | Select -Expand ProcessName -First 1}},@{n='CPU';e={($_.Group | Measure-Object CPU -Sum).Sum}} |
Sort-Object -Property cpu -Descending
プロパティ 内容 単位
CPU(s) すべてのプロセッサで使用したプロセッサ時間 秒単位
Handles オープンしたハンドルの数
ID プロセスID(PID)
NPM(K) 使用している非ページメモリのサイズ キロバイト単位
PM(K) 使用しているページ可能なメモリのサイズ キロバイト単位
ProcessName プロセスの名前
VM(M) 使用している仮想メモリ(ディスク上のページングファイルの記憶領域)のサイズ メガバイト単位
WS(K) ワーキングセット(最近参照したメモリのページ)のサイズ キロバイト単位

アプリケーション

アプリケーション一覧

Get-ItemProperty HKLM:Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Comments, Publisher, InstallDate | Format-Table -Property DisplayName,Publisher,DisplayVersion,InstallDate

Explorer

$path = [System.Environment]::GetFolderPath("Desktop")

Start-Process $path
$path = [System.Environment]::GetFolderPath("MyDocuments")

Invoke-Item $path

ブラウザ

既定のブラウザ

start http://google.com

Edge

start microsoft-edge:http://google.com
Start-Process microsoft-edge:https://google.com -WindowStyle maximized

[System.Diagnostics.Process]::Start("C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe", "-InPrivate https://google.com")

Firefox

Start-Process 'C:\Program Files\Mozilla Firefox\firefox.exe' -argumentlist "-url http://google.com"

[System.Diagnostics.Process]::Start("C:\Program Files\Mozilla Firefox\firefox.exe", "-private-window https://google.com")

Office

Excel

$excel = New-Object -ComObject Excel.Application
$excel.Workbooks.Open("C:\Users\Y\Desktop\excel.xlsx")
$excel.Visible = $true
$excel.DisplayAlerts = $False

$excel.Quit()
ファイル読み書き

# Excelを開く
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$excel.DisplayAlerts = $False

# ファイルを開く
$book = $excel.Workbooks.Open("C:\Users\Y\Desktop\excel.xlsx")

# 名前を付けて保存
$book.SaveAs("C:\Users\Y\Desktop\excel2.xlsx")

# 閉じる
$excel.Quit()
$excel = $Null
[GC]::collect()
# Excelを開く
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$excel.DisplayAlerts = $False

### 新規作成
$book = $excel.Workbooks.add()

# 名前を付けて保存
$book.SaveAs("C:\Users\Y\Desktop\excel2.xlsx")

# 閉じる
$excel.Quit()
$excel = $Null
[GC]::collect()
セル編集

# Excelを開く
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$excel.DisplayAlerts = $False

### 新規作成
$book = $excel.Workbooks.add()
$sheet = $book.ActiveSheet

# セル編集
$sheet.Cells.Item(1,1) = "A1"
$sheet.Range("A1") = "A1A1"

$sheet.Cells.Item(1,1).Font.Bold = $True
$sheet.Cells.Item(1,1).Font.Size = 24
$sheet.Cells.Item(1,1).font.ColorIndex = 3
$sheet.Cells.Item(1,1).font.Name = "MS 明朝"
$sheet.Cells.Item(1,1).interior.ColorIndex = 6

# $sheet.Range("A2") = "=A1+1"
$sheet.Cells.Item(2,1) = "=A1&A1"

# 名前を付けて保存
$book.SaveAs("C:\Users\Y\Desktop\excel2.xlsx")

# 閉じる
$excel.Quit()
$excel = $Null
[GC]::collect()
PowerPoint

$ppt = New-Object -ComObject PowerPoint.Application
$ppt.Presentations.open("C:\Users\Y\Desktop\ppt.pptx")
# `$ppt.visible = $true` なしで起動

$ppt.Quit()
Word

$word = New-Object -ComObject word.application
$Word.documents.open("C:\Users\Y\Desktop\word.docx")
$word.visible = $true

$word.Quit()

SQL Server

その他アプリケーション

音声合成

$sapi = New-Object -com SAPI.SpVoice

$sapi.Rate     = 10 # -10 <= Rate <= 10
$sapi.Volume   =  0 # 0 <= Volume <= 100
$sapi.Voice = $sapi.GetVoices().Item(0)
# $sapi.Voice = $sapi.GetVoices().Item(1)

# $sapi.Speak("本日は、晴天なり。", 1) # Async
$sapi.Speak("本日は、晴天なり。") # Sync

サービス

日付処理

# 書式
$format = "yyyyMMdd-HHmmss"

Get-Date -DisplayHint Date     # 2022年5月23日
Get-Date -DisplayHint Time     # 19:45:50
Get-Date -DisplayHint DateTime # 2022年5月23日 19:45:50

Get-Date -Format $format       # 20220523-194431
Get-Date -Format "yyyy/MM/dd"  # 2022/05/23

# 要素( Get-Date|Get-Member )
(Get-Date).Year  #
(Get-Date).Month #
(Get-Date).day   #

# 明日
Write-Host (Get-Date).AddDays(1).ToString($format)

# 月末
$Today=(Get-Date)
(Get-Date -Year $Today.year -month $Today.month -Date 1).AddMonths(1).AddDays(-1)

# 文字列からキャスト
$datetime = [DateTime]::ParseExact("20150101-013000",$format, $null)
Write-Host $datetime.ToString($format)

日時書式指定子

標準日時形式文字列

書式指定子 説明 使用例
d 短い形式の日付パターン。 2009-06-15T13:45:30 -> 6/15/2009 (en-US)
2009-06-15T13:45:30 -> 2009/06/15
D 長い形式の日付パターン。 2009-06-15T13:45:30 -> Monday, June 15, 2009 (en-US)
2009-06-15T13:45:30 -> 2009年6月15日
f 完全な日付と時刻のパターン (短い形式の時刻)。 2009-06-15T13:45:30 -> Monday, June 15, 2009 1:45 PM (en-US)
2009-06-15T13:45:30 -> 2009年6月15日 13:45
F 完全な日付と時刻のパターン (長い形式の時刻)。 2009-06-15T13:45:30 -> Monday, June 15, 2009 1:45:30 PM (en-US)
2009-06-15T13:45:30 -> 2009年6月15日 13:45:30
g 一般の日付と時刻のパターン (短い形式の時刻)。 2009-06-15T13:45:30 -> 6/15/2009 1:45 PM (en-US)
2009-06-15T13:45:30 -> 2009/06/15 13:45
G 一般の日付と時刻のパターン (長い形式の時刻)。 2009-06-15T13:45:30 -> 6/15/2009 1:45:30 PM (en-US)
2009-06-15T13:45:30 -> 2009/06/15 13:45:30
Mm 月日パターン。 2009-06-15T13:45:30 -> June 15 (en-US)
2009-06-15T13:45:30 -> 6月15日
Oo ラウンドトリップする日付と時刻のパターン。 2009-06-15T13:45:30 (DateTimeKind.Local) --> 2009-06-15T13:45:30.0000000-07:00 (en-US)
2009-06-15T13:45:30 (DateTimeKind.Utc) --> 2009-06-15T13:45:30.0000000Z
2009-06-15T13:45:30 (DateTimeKind.Unspecified) --> 2009-06-15T13:45:30.0000000
Rr RFC1123 パターン。 2009-06-15T13:45:30 -> Mon, 15 Jun 2009 13:45:30 GMT
s 並べ替え可能な日付と時刻のパターン。 2009-06-15T13:45:30 (DateTimeKind.Local) -> 2009-06-15T13:45:30
2009-06-15T13:45:30 (DateTimeKind.Utc) -> 2009-06-15T13:45:30
t 短い形式の時刻パターン。 2009-06-15T13:45:30 -> 1:45 PM (en-US)
2009-06-15T13:45:30 -> 13:45
T 長い形式の時刻パターン。 2009-06-15T13:45:30 -> 1:45:30 PM (en-US)
2009-06-15T13:45:30 -> 13:45:30
u 並べ替え可能な日付と時刻のパターン (世界時刻)。 2009-06-15T13:45:30 -> 2009-06-15 13:45:30Z
U 完全な日付と時刻のパターン (世界時刻)。 2009-06-15T13:45:30 -> Monday, June 15, 2009 8:45:30 PM (en-US)
2009-06-15T13:45:30 -> 2009年6月15日 4:45:30
Yy 年月パターン。 2009-06-15T13:45:30 -> June 2009 (en-US)
2009-06-15T13:45:30 -> 2009年6月
$thisDate = [datetime]::ParseExact("2009-06-15T13:45:30", "yyyy-MM-ddTHH:mm:ss", $null)
$thisDate.ToString("d")

カスタム日時形式文字列

書式指定子 説明 使用例
d 月の日にち (1 ~ 31)。 2009-06-01T13:45:30 -> 1
2009-06-15T13:45:30 -> 15
dd 月の日にち (01 ~ 31)。 2009-06-01T13:45:30 -> 01
2009-06-15T13:45:30 -> 15
ddd 曜日の省略名。 2009-06-15T13:45:30 -> Mon (en-US)
2009-06-15T13:45:30 -> 月
dddd 曜日の完全名。 2009-06-15T13:45:30 -> Monday (en-US)
2009-06-15T13:45:30 -> 月曜日
f 日時値の秒部分の 1/10。 2009-06-15T13:45:30.6170000 -> 6
ff 日時値の秒部分の 1/100。 2009-06-15T13:45:30.6170000 -> 61
fff 日時値の秒部分の 1/1000。 2009-06-15T13:45:30.6170000 -> 617
ffff 日時値の秒部分の 1/10000。 2009-06-15T13:45:30.6175000 -> 6175
fffff 日時値の秒部分の 1/100000。 2009-06-15T13:45:30.6175400 -> 61754
ffffff 日時値の秒部分の 1/1000000。 2009-06-15T13:45:30.6175420 -> 617542
fffffff 日時値の秒部分の 1/10000000。 2009-06-15T13:45:30.6175425 -> 6175425
F 日時値の秒部分の 1/10 (0 以外の場合)。 2009-06-15T13:45:30.6170000 -> 6
FF 日時値の秒部分の 1/100 (0 以外の場合)。 2009-06-15T13:45:30.6170000 -> 61
FFF 日時値の秒部分の 1/1000 (0 以外の場合)。 2009-06-15T13:45:30.6170000 -> 617
FFFF 日時値の秒部分の 1/10000 (0 以外の場合)。 2009-06-15T13:45:30.5275000 -> 5275
FFFFF 日時値の秒部分の 1/100000 (0 以外の場合)。 2009-06-15T13:45:30.6175400 -> 61754
FFFFFF 日時値の秒部分の 1/1000000 (0 以外の場合)。 2009-06-15T13:45:30.6175420 -> 617542
FFFFFFF 日時値の秒部分の 1/10000000 (0 以外の場合)。 2009-06-15T13:45:30.6175425 -> 6175425
gg 時期または時代 (年号)。 2009-06-15T13:45:30.6170000 -> A.D. (en-US)
2009-06-15T13:45:30.6170000 -> 西暦
h 12 時間形式の時間 (1 ~ 12)。 2009-06-15T01:45:30 -> 1
hh 12 時間形式の時間 (01 ~ 12)。 2009-06-15T01:45:30 -> 01
H 24 時間形式の時間 (0 ~ 23)。 2009-06-15T01:45:30 -> 1
HH 24 時間形式の時間 (00 ~ 23)。 2009-06-15T01:45:30 -> 01
K タイム ゾーン情報。 2009-06-15T13:45:30, Kind Unspecified ->
2009-06-15T13:45:30, Kind Utc -> Z
2009-06-15T13:45:30, Kind Local -> -07:00 (en-US)
m 分 (0 ~ 59)。 2009-06-15T01:09:30 -> 9
mm 分 (00 ~ 59)。 2009-06-15T01:09:30 -> 09
M 月 (1 ~ 12)。 2009-06-15T13:45:30 -> 6
MM 月 (01 ~ 12)。 2009-06-15T13:45:30 -> 06
MMM 月の省略名。 2009-06-15T13:45:30 -> Jun (en-US)
MMMM 月の完全名。 2009-06-15T13:45:30 -> June (en-US)
s 秒 (0 ~ 59)。 2009-06-15T13:45:09 -> 9
ss 秒 (00 ~ 59)。 2009-06-15T13:45:09 -> 09
t AM/PM 指定子の最初の文字。 2009-06-15T13:45:30 -> P (en-US)
2009-06-15T13:45:30 -> 午
tt AM/PM 指定子。 2009-06-15T13:45:30 -> PM (en-US)
2009-06-15T13:45:30 -> 午後 (ja-JP)
y 年 (0 ~ 99)。 0001-01-01T00:00:00 -> 1
0900-01-01T00:00:00 -> 0
1900-01-01T00:00:00 -> 0
2009-06-15T13:45:30 -> 9
2019-06-15T13:45:30 -> 19
yy 年 (00 ~ 99)。 0001-01-01T00:00:00 -> 01
0900-01-01T00:00:00 -> 00
1900-01-01T00:00:00 -> 00
2019-06-15T13:45:30 -> 19
yyy 年 (3 桁以上)。 0001-01-01T00:00:00 -> 001
0900-01-01T00:00:00 -> 900
1900-01-01T00:00:00 -> 1900
2009-06-15T13:45:30 -> 2009
yyyy 年 (4 桁の数値)。 0001-01-01T00:00:00 -> 0001
0900-01-01T00:00:00 -> 0900
1900-01-01T00:00:00 -> 1900
2009-06-15T13:45:30 -> 2009
yyyyy 年 (5 桁の数値)。 0001-01-01T00:00:00 -> 00001
2009-06-15T13:45:30 -> 02009
z UTC を基準とする時間単位のオフセット (先行ゼロなし)。 2009-06-15T13:45:30-07:00 -> -7 (en-US)
zz UTC を基準とする時間単位のオフセット (先行ゼロ付きの 1 桁の値)。 2009-06-15T13:45:30-07:00 -> -07 (en-US)
zzz UTC を基準とする時間および分単位のオフセット。 2009-06-15T13:45:30-07:00 -> -07:00 (en-US)
: 時刻の区切り記号。 2009-06-15T13:45:30 -> : (en-US)
2009-06-15T13:45:30 -> . (it-IT)
2009-06-15T13:45:30 -> : (ja-JP)
/ 日付の区切り記号。 2009-06-15T13:45:30 -> / (en-US)
2009-06-15T13:45:30 -> / (ja-JP)
"文字列" '文字列' リテラル文字列の区切り記号。 2009-06-15T13:45:30 ("arr:" h:m t) -> arr: 1:45 P
% 後続の文字をカスタム書式指定子として定義します。 2009-06-15T13:45:30 (%h) -> 1
\ エスケープ文字。 2009-06-15T13:45:30 (h \h) -> 1 h
その他の文字 文字が結果の文字列にそのままコピーされます。 2009-06-15T01:45:30 (arr hh:mm t) -> arr 01:45 A
$thisDate = [datetime]::ParseExact("2009-06-15T13:45:30", "yyyy-MM-ddTHH:mm:ss", $null)
$thisDate.ToString("ddd")

日付の差

$since    = [DateTime]::ParseExact("2022/02/28 01:23:45","yyyy/MM/dd HH:mm:ss", $null)
$until    = [DateTime]::ParseExact("2022/03/01 12:34:56","yyyy/MM/dd HH:mm:ss", $null)
$timeDiff = $until - $since

$timeDiff.Days
$timeDiff.Hours
$timeDiff.Minutes
$timeDiff.Seconds

1

11

11

11

和暦・元号

西暦→和暦

using namespace System.Globalization

$dtString = "1873/01/01"

# $dt = ([datetime]$dtString)
$dt = $dtString -as [datetime]

$j = [cultureinfo]::new("ja_JP")
$j.DateTimeFormat.Calendar = [JapaneseCalendar]::new()
$dt.ToString("ggy年M月d日", $j)

明治6年1月1日

和暦→西暦

using namespace System.Globalization

$dtString = "明治6年1月1日"

$j = [cultureinfo]::new("ja_JP")
$j.DateTimeFormat.Calendar = [JapaneseCalendar]::new()
$dt = [datetime]::ParseExact($dtString, "ggy年M月d日", $j)
$dt.ToString("yyyy/MM/dd")

1873/01/01

和暦の格納先(レジストリ)

Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Control\Nls\Calendars\Japanese\Eras

1868 01 01 : 明治_明_Meiji_M

1912 07 30 : 大正_大_Taisho_T

1926 12 25 : 昭和_昭_Showa_S

1989 01 08 : 平成_平_Heisei_H

2019 05 01 : 令和_令_Reiwa_R

PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\Calendars\Japanese\Eras

PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\Calendars\Japanese

PSChildName : Eras

PSDrive : HKLM

PSProvider : Microsoft.PowerShell.Core\Registry

ファイル操作

カレントディレクトリ

# 実行中のパス取得/移動
[string] $dirPath = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $dirPath

特別なフォルダのパスを取得

$path = [System.Environment]::GetFolderPath("Desktop")

# 特別なフォルダの一覧
[System.Enum]::GetValues([System.Environment+SpecialFolder])

ファイル一覧

$dirPath = [System.Environment]::GetFolderPath("Desktop")

Get-ChildItem -Path $dirPath -Recurse

# ソートに使うプロパティ、表示件数を指定
Get-ChildItem $dirPath -Recurse | Sort-Object "Name" -Descending | Select-Object -First 10

フォルダの合計サイズ(再帰)

$dirPath = [System.Environment]::GetFolderPath("Desktop")

Function Format-FileSize() {
    Param ([int64]$size)
    If     ($size -gt 1TB) {[string]::Format("{0:0.00} TB", $size / 1TB)}
    ElseIf ($size -gt 1GB) {[string]::Format("{0:0.00} GB", $size / 1GB)}
    ElseIf ($size -gt 1MB) {[string]::Format("{0:0.00} MB", $size / 1MB)}
    ElseIf ($size -gt 1KB) {[string]::Format("{0:0.00} kB", $size / 1KB)}
    ElseIf ($size -gt 0)   {[string]::Format("{0:0.00} B", $size)}
    Else                   {""}
}

Get-ChildItem -Path $dirPath -Recurse -Force | ForEach-Object -Begin { $Len = 0 } -Process { $Len = $Len + $_.Length } -End { write-host ("total size is " + (Format-FileSize $Len)) }

ファイルフォルダの新規作成

[string] $filepath = Convert-Path ".\test_new.txt"
[string] $dirPath = Split-Path -Parent $MyInvocation.MyCommand.Path + "\newDir"

New-Item c:\tmp\itemized.csv -itemType File
New-Item .\temp -itemType Directory

touch

Function touch($file) {
  If (Test-Path $file) {
    (Get-Item $file).LastWriteTime = Get-Date
  } Else {
    Out-File -encoding Default $file
  }
}

呼出し例:

touch test_touch.txt

ファイルフォルダの削除

[string] $filepathToRemove = Convert-Path ".\test_new.txt"
[string] $dirPathToRemove = Split-Path -Parent $MyInvocation.MyCommand.Path + "\newDir"

if( $(Test-Path $filepath) -ne $True ){
    Remove-Item $filepath
}
if( $(Test-Path $dirPath) -ne $True ){
    Remove-Item $dirPath -Recurse -Force
}

ファイルフォルダのコピーと移動

[string] $filepathToRemove = Convert-Path ".\test_new.txt"

Copy-Item -Confirm $filepathToRemove $filepathToRemove+".bak"
Move-Item -force $filepathToRemove $filepathToRemove+".bak"

ファイルの作成日時・最終更新日時・最終アクセス日時

取得

$file = ".\test.txt"

Out-File -encoding Default $file

$fc = Get-ChildItem $file
$fi = Get-ItemProperty $file

# 作成時刻
$fc.CreationTime
$fi.CreationTime

# 最終更新時刻
$fc.LastWriteTime
$fi.LastWriteTime

# 最終アクセス時刻
$fc.LastAccessTime
$fi.LastAccessTime

設定

$file = ".\test.txt"

# 作成時刻
Set-ItemProperty $file CreationTime $(Get-Date)
Set-ItemProperty $file CreationTime $("2022/04/16 12:34:56")

# 最終更新時刻
Set-ItemProperty $file LastWriteTime $(Get-Date)
Set-ItemProperty $file LastWriteTime $("2022/04/16 12:34:56")

# 最終アクセス時刻
Set-ItemProperty $file LastAccessTime $(Get-Date)
Set-ItemProperty $file LastAccessTime $("2022/04/16 12:34:56")

テキストファイル

文字コードについては、「-Encoding Default」と指定すればShift_JIS、「-Encoding UTF8」と指定すればUTF-8で読み書きされる。

1行ごとに読込み

[string] $filepath = Convert-Path ".\test_read.txt"
Write-Host $filepath

if (Split-Path $filepath -Parent) {
    if (Test-Path $filepath) {
        $i=1

        #

        $f = (Get-Content $filepath) -as [string[]]
        foreach ($l in $f) {
            Write-Host $i : $l
            $i++
        }

        # OR

        foreach ($l in Get-Content $filepath) {
            Write-Host $i : $l
            $i++
        }

        # OR

        Get-Content $filepath -Encoding UTF8 | ForEach-Object {
            $l = $_
        }

        # OR

        $file = New-Object System.IO.StreamReader($filepath, [System.Text.Encoding]::GetEncoding("sjis"))
        while (($line = $file.ReadLine()) -ne $null) {
            Write-Host($line)
        }
        $file.Close()
    }
}

書出し

[string] $filepath = Convert-Path ".\test_write.txt"
$data = "出力内容`n";

#

$text > $filepath;

# OR

$text | Out-File $filepath;

# OR

[IO.File]::WriteAllLines($filepath,$text);

# OR

[IO.File]::WriteAllText($filepath,$text);

# OR

$file = New-Object System.IO.StreamWriter($filepath, $false, [System.Text.Encoding]::GetEncoding("sjis"))
$file.WriteLine($data)
$file.WriteLine($data)
$file.Close()

# OR

# ファイルの末尾に追記
Add-Content $filepath $text

JSONファイル

読込み

$json = ConvertFrom-Json -InputObject (Get-Content .\sample.json -Raw)
Write-Host $json.key1

書込み

$jsonStr = @{
  "Foo Bar" = @{Key1=111; Key2="222"};
  "Hoge Piyo" = @{Key1=222; Key2="222"};
}

$jsonStr | ConvertTo-Json | Out-File .\sample.json -Encoding utf8 -Append
$jsonStr | ConvertTo-Json -Compress | Out-File .\sample.json -Encoding utf8 -Append

CSVファイル

読込み

[string] $filepath = Convert-Path ".\test_read.csv"
Write-Host $filepath

if (Split-Path $filepath -Parent) {
    if (Test-Path $filepath) {
        # ヘッダ行のあるCSVファイルを読み込む場合
        #$c = Import-Csv $filepath -Encoding Default

        # ヘッダ行のないCSVファイルを読み込む場合
        #$c = Import-Csv $filepath -Encoding Default -Header (.4)
        #$c = Import-Csv $filepath -Encoding Default -Header "name1", "name2", "name3"

        # ヘッダ行のないCSVファイルを読み込み、列名を指定して表示する場合
        #$c = Import-Csv $filepath -Encoding Default -Header "name1", "name2", "name3" | Select-Object "name1"

        # ヘッダ行のないCSVファイルを読み込み、列名および行の値を指定して表示する場合
        $c = Import-Csv $filepath -Encoding Default -Header (.3) | Where-Object {
            $_.1 -eq "1"
        }

        # 取得したデータの各行ごとに処理を行う場合
        $i=1
        $c | ForEach-Object {
            Write-Host $i : $_ : $_.1 : $_.2 : $_.3
        }
    }
}

書出し

[string] $filepath = Convert-Path ".\test_write.csv"
$array = @(
    @{ID=1; TITLE="AAA"; PRICE=1000},
    @{ID=2; NAME="BBB"; PRICE=2000},
    @{ID=3; NAME="CCC"; PRICE=3000}
)
$array | % {New-Object PSObject -Property $_} | Export-Csv $filepath -Encoding Default  # Shift-JIS
[string] $filepath = Convert-Path ".\test_write.csv"

# カスタムオブジェクトを作成
$Datas = @()

# 行の追加
$Data = New-Object PSObject | Select-Object AAA, BBB, CCC # 列を作成
$Data.AAA = "1, 1"
$Data.BBB = "1, 2"
$Data.CCC = "1, 3"
$Datas += $Data

# 行の追加
$Data = New-Object PSObject | Select-Object AAA, BBB, CCC
$Data.AAA = "2, 1"
$Data.BBB = "2, 2"
$Data.CCC = "2, 3"
$Datas += $Data

# カスタムオブジェクトを確認
$Datas | ft -AutoSize

$Datas | Export-Csv $filepath -Encoding Default  # Shift-JIS

設定ファイル(.ini)

読込み

function Get-Config {
    $Path = "settings.ini"

    $Config = @{}
    Get-Content $Path | %{ $Config += ConvertFrom-StringData $_ }

    return $Config
}
USERNAME[email protected]
PASSWORD=foobar

コメント行対応

[string] $filepath = Convert-Path ".\test_read.ini"

$lines = Get-Content $filepath
foreach($line in $lines){
    if($line -match "^$" -or $line -match "^\s*;"){ continue }

    $kv = $line.split("=",2)
    Write-Host("key : " + $kv[0])
    Write-Host("val : " + $kv[1])
}
; コメント行
key1=value1
key2=value2
key3=value3

構成ファイル(Web.config)

読み書き

Add-Type -AssemblyName System.Configuration

$Map = New-Object System.Configuration.ExeConfigurationFileMap
$Map.ExeConfigFilename = ".\Web.config"
$Config = [System.Configuration.ConfigurationManager]::OpenMappedExeConfiguration($Map, [System.Configuration.ConfigurationUserLevel]::None)

$Setting = $Config.AppSettings.Settings["MY_KEY"]
If ($Setting -eq $null) {
    $Config.AppSettings.Settings.Add("MY_KEY", "New Value1");
} else {
    $Setting.Value = "New Value2";
}

$Config.Save();
<?xml version="0" encoding="utf-8"?>
<configuration>
    <appSettings>
        <add key="MY_KEY_DEFAULT" value="Current Value" />
        <add key="MY_KEY" value="New Value2" />
    </appSettings>
</configuration>

XMLファイル

読込み

$xml = New-Object System.Xml.XmlDocument
$xml.Load(".\test_read.xml")
Write-Output $xml.item.itemitem

# OR

[string] $filepath = Convert-Path ".\test_read.xml"
$xmlString = [xml](Get-Content $filepath)

# OR

$xmlString = @'
<item>
  <itemitem>value</itemitem>
</item>
'@

$xml = [xml]$xmlString
 Write-Host $xml.item.itemitem

書出し

[string] $filepath = Convert-Path ".\test_write.xml"

$xml = New-Object System.Xml.XmlDocument

$item = $xml.CreateElement('item')
$itemitem = $xml.CreateElement('itemitem')
$itemitem.SetAttribute('attr1', 'value1')

$item.AppendChild($itemitem)
$xml.AppendChild($item)

$xml.Save($filepath)

ZIPファイル

圧縮

パラメーター 内容
-Force 既存ファイルが存在したら上書き
-Update 既存ファイルを更新
-CompressionLevel 圧縮の量
Optimal 最適値
Fastest 使用可能な最速の圧縮方法
NoCompression 圧縮しない
-LiteralPath Path パラメーターとは異なり、ワイルドカードを利用しない場合に指定
Compress-Archive -Path .\*.ps1 -DestinationPath .\archive.zip

Compress-Archive -LiteralPath .\foo.ps1,.\bar.ps1 -CompressionLevel Optimal -DestinationPath .\archive.zip

解凍

$date = Get-Date -Format "yyyyMMddHHmmss"
New-Item .\$date -ItemType Directory -Force
Expand-Archive -Path .\archive.zip -DestinationPath .\$date
Get-ChildItem .\*.zip | Expand-Archive -DestinationPath .\

ログ出力

ログファイル

$title = "Title"
$format = "yyyyMMdd"

[string] $filepath = ( Split-Path -Parent $MyInvocation.MyCommand.Path ) + "\Logs\"
Write-Host $filepath
if (Test-Path $filepath) {
    Set-Location $filepath
    [string] $filename = $title + "_" + (Get-Date).ToString($format) + ".log"
    Write-Output($filepath + $filename)

    Write-Host("Add-Content 追記/新規作成 開始")
    Write-Output test | Add-Content -Encoding Default $filename
    Write-Host("Add-Content 追記/新規作成 完了")
}

イベントログ

$format = "yyyy/MM/dd HH:mm:ss"
$startTime = Write-Host (Get-Date).ToString($format)
$endTime = Write-Host (Get-Date).ToString($format)

# エラーレコードと情報レコードで共通の項目
$logName        = "Application"
$source         = "ES_ProfileWrite"

# エラーレコードと情報レコードで異なる項目
$eventIdError   = "50008"
$eventIdInfo    = "50007"
$eventTypeError = "Err"
$eventTypeInfo  = "Info"
$messageError   = "エラーが発生しました。ログを確認してください。(処理開始:${startTime}、処理終了:${endTime})"
$messageInfo    = "処理が正常に完了しました。(処理開始:${startTime}、処理終了:${endTime})"

# ソースが既に存在するか確認
if ([System.Diagnostics.EventLog]::SourceExists($source) -eq $false){
    New-EventLog -LogName $logName -Source $source
}

# イベントログを出力
Write-EventLog -EntryType $eventTypeInfo  -EventId $eventIdInfo  -LogName $logName -Message $messageInfo  -Source $source
Write-EventLog -EntryType $eventTypeError -EventId $eventIdError -LogName $logName -Message $messageError -Source $source

# ソースを削除する場合
# Remove-EventLog -Source $source

# イベントログを取得
Get-WinEvent -FilterHashtable @{
    LogName='System', 'Application'
    Level=1,2,3
    StartTime=(Get-Date).Date.AddDays(-1)
} |
Select-Object -Property * | Format-Table -AutoSize -Wrap -Property TimeCreated,Id,Level,LevelDisplayName,ProviderName

Level キー

Name Value
Verbose 5
Informational 4
Warning 3
Error 2
Critical 1
LogAlways 0

ISOファイル

マウント

$isoPath = "C:\disc1.iso"

Mount-DiskImage $isoPath
$driveLetter = (Get-DiskImage -ImagePath $isoPath | Get-Volume).DriveLetter
"{0}:\" -f $driveLetter | Write-Output

アンマウント

$isoPath = "C:\disc1.iso"

DisMount-DiskImage $isoPath

画像ファイル

画像編集

画像のトリミング

$SourcePath = ".\trim.jpg";
$DestinationPath = ".\trimmed.png";

Add-Type -AssemblyName System.Drawing
$SourceBitmap = New-Object System.Drawing.Bitmap($SourcePath)

# x, y, width, height
$TrimRectangle = New-Object System.Drawing.Rectangle(240, 670, 600, 400);
$DestinationBitmap = $SourceBitmap.Clone($TrimRectangle, $SourceBitmap.PixelFormat)
$DestinationBitmap.Save($DestinationPath, [System.Drawing.Imaging.ImageFormat]::Png)

$DestinationBitmap.Dispose()
$SourceBitmap.Dispose()

スクリーンショット

ネットワーク

URLエンコード

$url = 'http://httpbin.org/get?query=あいうえお 12345 abc'

$escapedData = [Uri]::EscapeDataString($url)
Write-Host $escapedData
$unescapedData = [Uri]::UnescapeDataString($escapedData)

$escapedUri = [Uri]::EscapeUriString($url)
Write-Host $escapedUri
$unescapedUri = [Uri]::UnescapeDataString($escapedUri)

Add-Type -AssemblyName System.Web
$escapedUrl = [System.Web.HttpUtility]::UrlEncode($url)
Write-Host $escapedUrl
$unescapedUrl = [System.Web.HttpUtility]::UrlDecode($escapedUrl)

http%3A%2F%2Fhttpbin.org%2Fget%3Fquery%3D%E3%81%82%E3%81%84%E3%81%86%E3%81%88%E3%81%8A%E3%80%8012345%20abc

http://httpbin.org/get?query=%E3%81%82%E3%81%84%E3%81%86%E3%81%88%E3%81%8A%E3%80%8012345%20abc

http%3a%2f%2fhttpbin.org%2fget%3fquery%3d%e3%81%82%e3%81%84%e3%81%86%e3%81%88%e3%81%8a%e3%80%8012345+abc

ping

Test-Connection -computername "localhost" -count 1 -Quiet

Test-Connection -computername "localhost" -count 1

Test-Connection -computername "nohost" -count 1 -Quiet # 存在しないホスト

Test-Connection -computername "nohost" -count 1

True

Destination: localhost

Ping Source Address Latency BufferSize Status

                                               (ms)        (B)

1 ******* ::1 0 32 Success

Flase

Test-Connection: Testing connection to computer 'localhos' failed: Cannot resolve the target name.

ARP

Get-NetNeighbor

ifIndex IPAddress LinkLayerAddress State PolicyStore


16 192.168.0.255 FF-FF-FF-FF-FF-FF Permanent ActiveStore

Webリクエスト

GET

# http://httpbin.org/get?q=qwerty
$url = 'http://httpbin.org/get'
$params = @{"q"="qwerty"}
$headers = @{Authorization="Bearer token"; accept="application/json"}


# $url = 'http://example.exampleexample'


try {
  $res = Invoke-WebRequest $url -Body $params  -Headers $headers
  $res.StatusCode.ToString() + " " + $res.StatusDescription
  Write-Host "Headers" $res.Headers
  Write-Host "Content" $res.Content
} catch {
  Write-Host $_.Exception
}
# http://httpbin.org/get?q=qwerty
$url = 'http://httpbin.org/get'
$params = @{"q"="qwerty"}
$headers = @{Authorization="Bearer token"; accept="application/json"}

$res = Invoke-RestMethod $url -Headers $headers -Body $params
$res | FL

ファイルをダウンロードする

$url = 'https://raw.githubusercontent.com/pandas-dev/pandas/main/pandas/tests/io/data/csv/iris.csv'
Invoke-WebRequest $url -Body $params -OutFile '.\iris.csv'

リンクURLの一覧を取得する

$url = 'http://www.google.co.jp'
$res = Invoke-WebRequest $url

$links = $res.Links
$links | Where-Object {(($_.href -like "http://www.google.co.jp/intl/*") -or ($_.href -like "https://accounts.google.com/*"))} | Select-Object -ExpandProperty href

POST

# http://httpbin.org/post
$url = 'http://httpbin.org/post'

Invoke-WebRequest -Method Post -Body 'キー1=値1&キー2=値2' $url
Invoke-WebRequest -Method Post -Body @{キー1="値1"; キー2="値2"} $url # 連想配列の場合はマルチバイト文字がエスケープされる
Invoke-WebRequest -Method Post -Body '{ "key1":"val1", "key2":"val2" } }' $url

Invoke-WebRequest -Method Post -InFile src/icon.png $url
# http://httpbin.org/post
$url = 'http://httpbin.org/post'
$params = @{"q"="qwerty"}
$headers = @{Authorization="Bearer token"; accept="application/json"}

$res = Invoke-RestMethod $url -Method 'POST' -Headers $headers -Body $params
$res | FL

ネットワークアダプタ

# ネットワークアダプター一覧
Get-NetAdapter | Format-Table
Get-NetAdapter | ? {$_.Name -eq "Wi-Fi"}
# ネットワークアダプターのプロパティ
Get-NetAdapterBinding | Format-Table

# 設定変更
# Set-NetAdapterBinding -Name "Wi-Fi" -DisplayName "Internet Protocol Version 4 (TCP/IPv4)" -Enabled $True
Set-NetAdapterBinding -Name "Wi-Fi" -DisplayName "インターネット プロトコル バージョン 4 (TCP/IPv4)" -Enabled $True
Set-NetAdapterBinding -Name "Wi-Fi" -ComponentID ms_tcpip -Enabled $True
# ネットワークアダプターの詳細設定
Get-NetAdapterAdvancedProperty | Format-Table

IPアドレス

# IPアドレスを取得
Get-NetIPAddress | Format-Table InterfaceAlias, IPAddress

# IPアドレス・デフォルトゲートウェイ・DNSサーバーを変更(削除→追加)
Remove-NetIPAddress -IPAddress '192.168.1.000'
New-NetIPAddress -InterfaceIndex 00 -AddressFamily IPv4 -IPAddress '192.168.1.000' -PrefixLength 24
Remove-NetRoute -InterfaceIndex 00 -NextHop 192.168.1.000
New-NetRoute -InterfaceIndex 00 -DestinationPrefix '0.0.0.0/0' -AddressFamily IPv4 -NextHop '192.168.1.000' -RouteMetric 0
Set-DnsClientServerAddress -InterfaceIndex 00 -ServerAddresses 192.168.1.000

Copyright (c) 2022 YA-androidapp(https://github.com/YA-androidapp) All rights reserved.

About

PowerShellチートシート

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published