powershell

powershellでメッセージボックス

# アセンブリの読み込み Add-Type -Assembly System.Windows.Forms # メッセージボックスの表示 [System.Windows.Forms.MessageBox]::Show("ここにメッセージ", "タイトル") ネタ元 https://letspowershell.blogspot.com/2015/06/powershellmessagebox.html

powershellの例外

普通にtry-catch https://qiita.com/NakaD/items/6979223ef9e6560d047c

powershellで復元ポイント作成

yyyymmddで名前つけて作成checkpoint.ps1 $formatted_date = (Get-Date).ToString("yyyyMMdd"); CheckPoint-Computer -Description $formatted_datevbsから実行するときは管理者権限つけてみよう ExecutionPolicy も指定しないと実行できな環境もある Set ob…

powershellで復元ポイントの作成

CheckPoint-Computerコマンド管理者権限で起動する必要あり ネタ元 https://blog.powershell-from.jp/?p=1947

powershellでCSVを読み込んで必要な項目だけ出力 あと日時を日付だけの表記にするのもやっちゃうスクリプト

#ヘッダが無いファイルはヘッダも指定する $csv = Import-Csv tosho-etf-stock-prices_2016_1305.csv -Encoding Default -Header "SC","名称","市場","業種","日時","株価","前日比","前日比(%)","前日終値","始値","高値","安値","出来高","売買代金(千円)…

powershellでメール送信

$From="送信元メールアドレス" $Subject="納品書" $body="納品内容を送信致します。" # このファイルの場所を取得 $FilePath=Split-Path ( & { $myInvocation.ScriptName } ) -parent $File=@(Get-ChildItem $FilePath'\添付ファイル.zip') $To="宛先メール…

powershellのコメント

# ほげほげ <# ほげほげ #> ネタ元 http://bayashita.com/p/entry/show/36

powershellでポップアップ

$wsobj = new-object -comobject wscript.shell $result = $wsobj.popup("PowerShellでメッセージボックスを表示")ネタ元 http://win.just4fun.biz/PowerShell/PowerShell%E3%81%A7%E3%83%A1%E3%83%83%E3%82%BB%E3%83%BC%E3%82%B8%E3%83%9C%E3%83%83%E3%82%A…

powershell絶対パスからファイル名を取得する

Get-ChildItemコマンドレットは FileInfoオブジェクト を返します。 FileInfoクラスには 絶対パスからファイル名を取得するためのプロパティ Name がありますのでこれを使用します。 $(Get-ChildItem "C:\Work\HIRO1.mdf").Nameネタ元 http://hiros-dot.net/…

PowerShellスクリプトファイル(.ps1)にファイルをドラッグ&ドロップして処理する

普通にはできないのでbatファイルと組み合わせる。バッチファイル(.bat)記述例 ・example.bat @echo off powershell -ExecutionPolicy Unrestricted -File %0\..\example.ps1 %*スクリプトファイル(.ps1)記述例 ・example.ps1 foreach ($arg in $args) { // …

powershellでFTPファイルアップロード

引数でアップロードするファイル指定するバージョンhogehoge.ps1として保存 foreach ($arg in $args) { [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} $filename = $(Get-ChildItem $arg).Name #「検証プロシージャによ…

PowerShellで文字列操作

http://hiros-dot.net/PowerShell/stringMenu.htm

PowerShellでLeft,Right,Midをしたい場合・Substring

Mid $abc.Substring(5, 6)ネタ元 http://win.just4fun.biz/PowerShell/PowerShell%E3%81%A7Left,Right,Mid%E3%82%92%E3%81%97%E3%81%9F%E3%81%84%E5%A0%B4%E5%90%88%E3%83%BBSubstring.html#w63c72ed

Powershell で文字コードを変更する(clip.exe へのリダイレクトもね)

# S-JIS に変更 $OutputEncoding = [Console]::OutputEncoding # US-ASCII に戻す $OutputEncoding = New-Object System.Text.ASCIIEncodingclip.exe にリダイレクトすると クリップボードに送れるネタ元 http://www.vwnet.jp/Windows/PowerShell/CharCode.h…

powershellでshit-jisでのファイル出力

Encoding Defaultでshift-jis "弁士" | Out-File -FilePath test.txt -Encoding Default ネタ元 http://webcache.googleusercontent.com/search?q=cache:7vgKnej7QQkJ:bbs.wankuma.com/index.cgi%3Fmode%3Dal2%26namber%3D61122%26KLOG%3D102+&cd=2&hl=ja&ct…

Powershellのinvoke-webrequestを使ってファイルのダウンロード POST送信でもなんでもできるさ!

Invoke-RestMethod -Uri "http://localhost:8080/myapp/mydomain/myaction/" -Method POST -Body "id=2014&filename=0609.csv&format=json"ネタ元 http://bakemoji.hatenablog.jp/entry/2014/01/02/144102http://qiita.com/_y_u_/items/94dd8ad6fcc8d278f433

PowerShellからIEを操作

http://qiita.com/fujimohige/items/5aafe5604a943f74f6f0 http://www.webessentials.biz/powershell/powershell_autologin/

batファイルからPowerShellスクリプトを実行する

こんな感じ powershell Set-ExecutionPolicy RemoteSigned powershell .\main.ps1 powershell Set-ExecutionPolicy Restricted管理者権限が必要な処理の場合はbatファイルも管理者権限で起動する必要がある。ダブルクリックで管理者権限起動したい場合jはシ…

powershellでExcelファイルの個人情報を削除する

事前に Set-ExecutionPolicy RemoteSigned して PowerShellスクリプトの実行セキュリティ・ポリシーを変更しておく必要はある。 $path = "Xlsを探すパス(サブフォルダも再帰して探します)" Add-Type -AssemblyName Microsoft.Office.Interop.Excel $xlRemove…

powershellのはじめの一歩はCodeZineから

かなりわかりやすい Windows PowerShell 入門 (CodeZine) http://codezine.jp/article/corner/10

文字列操作 部分文字列の切り出しはSubString()

http://blogs.wankuma.com/mutaguchi/archive/2010/02/13/186034.aspx # 切り出し $substr = "abcd".SubString(0,2) # ab ネタ元 PowerShell基礎文法最速マスター

ファイルの削除はRemove-Item

Remove-Item <ファイルパス> ネタ元 http://www.atmarkit.co.jp/fwin2k/win2ktips/1069psffolder/psffolder.html

PowerShellのNULLチェックは -eq $null 

PowerShell では、$null という名前の特殊な変数で表します。 $var = Get-ChildItem *.txt | Select-Object Name if( $var -eq $null ){ Write-Host "No files" } ネタ元 http://ufcpp.net/study/powershell/variable.html

PowerShellの終了はexit

exit

PowerShellのコメントは#

文字列のスプリットは .split()

MSH C:\> "Microsoft Command Shell".split() Microsoft Command Shellネタ元 http://d.hatena.ne.jp/newpops/20051010/p1

特殊文字 改行コードは「`n」

エスケープ・シーケンス `b バックスペース `n 改行 `r キャリッジ・リターン `t タブ ネタ元 http://www.atmarkit.co.jp/fwin2k/operation/psh02/psh02_02.html

ファイル出力 "ファイルに書き込む文字列" | Out-File "出力ファイルパス"

PS C:\Work> "あいうえお" | Out-File sample.txt -encoding UTF8エンコード指定しないとUTF-16になるみたい。 これだとCSVで出力してもexcelでうまく認識しない。 ネタ元 http://hiros-dot.net/PowerShell/file/file04.htm

ファイル読み取って1行ずつ処理

Get-Content ./hogelist.txt | Foreach-Object { echo $_ } ネタ元 http://d.hatena.ne.jp/moto0215/searchdiary?word=%2A%5Bpowershell%5D

ファイル名の変更は Rename-Item 名前を変更する項目のパス -newName 項目の新しい名前

ネタ元 http://hiros-dot.net/PowerShell/file/file21.htm