LoginSignup
1
3

More than 1 year has passed since last update.

PowerShell オレオレ Tips 集

Last updated at Posted at 2020-06-22

たまに PowerShell を使うのですが、よく使うコマンドを覚えて忘れてしまう為、メモしておきます

環境

PS C:\Users\Administrator> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.17763.1007
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17763.1007
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

イベントログが見たい

Get-EventLog を使う

# LogName で指定出来るログの種類を確認
PS C:\Users\Administrator> Get-EventLog -List

  Max(K) Retain OverflowAction        Entries Log
  ------ ------ --------------        ------- ---
  20,480      0 OverwriteAsNeeded      26,894 Application
  20,480      0 OverwriteAsNeeded           0 HardwareEvents
     512      7 OverwriteOlder              0 Internet Explorer
  20,480      0 OverwriteAsNeeded           0 Key Management Service
  20,480      0 OverwriteAsNeeded      21,411 Security
  20,480      0 OverwriteAsNeeded       5,370 System
  15,360      0 OverwriteAsNeeded         954 Windows PowerShell

# 直近5件の System ログを見る
PS C:\Users\Administrator> Get-EventLog -LogName System -Newest 5

   Index Time          EntryType   Source                 InstanceID Message
   ----- ----          ---------   ------                 ---------- -------
   41972 Jun 22 04:09  Information Service Control M...   1073748860 The WinHTTP Web Proxy Auto-Discovery Servic...
   41971 Jun 22 04:03  Information Service Control M...   1073748860 The Network Setup Service service entered t...
   41970 Jun 22 04:03  Information Service Control M...   1073748860 The Network Setup Service service entered t...
   41969 Jun 22 03:37  Information Service Control M...   1073748860 The WinHTTP Web Proxy Auto-Discovery Servic...
   41968 Jun 22 03:25  Information Service Control M...   1073748860 The Windows Modules Installer service enter...

# 特定のログを Format-List でメッセージ詳細を見る
PS C:\Users\Administrator> Get-EventLog -LogName System -Index 41972 | Format-List


Index              : 41972
EntryType          : Information
InstanceId         : 1073748860
Message            : The WinHTTP Web Proxy Auto-Discovery Service service entered the running state.
Category           : (0)
CategoryNumber     : 0
ReplacementStrings : {WinHTTP Web Proxy Auto-Discovery Service, running}
Source             : Service Control Manager
TimeGenerated      : 6/22/2020 4:09:30 AM
TimeWritten        : 6/22/2020 4:09:30 AM
UserName           :

ファイルの中身を表示したい

cat が使えたのでこれを使う

PS C:\Users\Administrator> echo "hello" >> test.txt
PS C:\Users\Administrator> cat .\test.txt
hello

Get-Content でも良さそう。

PS C:\Users\Administrator> Get-Content .\test.txt
hello

特定ホストの特定ポートへの接続確認をしたい

nc がないので何かあるかと思ったら以下が便利そう

Test-NetConnection

# Port 443 による接続は OK
PS C:\Users\Administrator> Test-NetConnection www.google.co.jp -Port 443


ComputerName     : www.google.co.jp
RemoteAddress    : 216.58.197.195
RemotePort       : 443
InterfaceAlias   : Ethernet
SourceAddress    : 172.31.1.127
TcpTestSucceeded : True

# Port 22 による接続は NG
PS C:\Users\Administrator> Test-NetConnection www.google.co.jp -Port 22
WARNING: TCP connect to (216.58.197.195 : 22) failed


ComputerName           : www.google.co.jp
RemoteAddress          : 216.58.197.195
RemotePort             : 22
InterfaceAlias         : Ethernet
SourceAddress          : 172.31.1.127
PingSucceeded          : True
PingReplyDetails (RTT) : 7 ms
TcpTestSucceeded       : False

ファイルを編集をするためにメモ帳を開きたい

以下のように notepad.exe の引数に編集したいファイルのパスを記載する。

PS C:\Users\Administrator> notepad.exe .\test.txt

Invoke-Item でも開けるが、こちらはプログラムの実行のようなのでメモ帳を開くということなら上記のほうが良さそう

ファイルをダウンロードしたい

wget をしたい時にどうするか。

Invoke-WebRequestを使う。
以下例。

# 指定した URL から msi をダウンロードして amazon-cloudwatch-agent.msi としてカレントディレクトリに保存する
PS C:\Users\Administrator> Invoke-WebRequest -Uri "https://s3.amazonaws.com/amazoncloudwatch-agent/windows/amd64/lat
est/amazon-cloudwatch-agent.msi" -OutFile "amazon-cloudwatch-agent.msi"

msi をインストールしたい

未検証だが、以下でできそう

Powershell: Installing MSI files

環境変数を見たい

Get-ChildItem env: 確認出来る。

PS C:\Users\Administrator> Get-ChildItem env:

Name                           Value
----                           -----
ALLUSERSPROFILE                C:\ProgramData
APPDATA                        C:\Users\Administrator\AppData\Roaming

・・・

環境変数を設定したい

$env:<name> = <value> という形式で設定できる。

# TEST_EVN = False という環境変数を設定する
PS C:\Users\Administrator> $env:TEST_ENV = "False"

# 設定されている事を確認する
PS C:\Users\Administrator> Get-ChildItem env:

・・・

TEST_ENV                       False

オブジェクトの構造を見たい

Get-member を使う

オブジェクトの構造を表示する (Get-member)

AWS Black Belt Tech シリーズ 2015 AWS CLI & AWS Tools for Windows Powershell

プロセスを見たい

tasklist コマンドで見る

grep したい

Select-String を使う

PS C:\Users\Administrator> tasklist | Select-String "hogefuga"
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3