【PowerShell:リファレンス&使用例】Add-Contentコマンドレット

目次

PowerShellのコマンドレット「Add-Content」

説明

指定した項目に内容を追加します。たとえば、ファイルに語を追加します。

書式

・構文
Add-Content
[-Path] <string[]>
[-Value] <Object[]>
[-PassThru] [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-Force] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-NoNewline] [-Encoding <Encoding>] [-AsByteStream] [-Stream <string>] [<CommonParameters>]

エイリアス

ac

使用例

使用例①今日の日付(get-dateの出力結果)を指定したテキストファイルの最後に追加して出力します。

 

PS C:\temp> more today.txt
2022/12/04 13:45:47          ←実行前の内容

PS C:\temp> get-date|Add-Content -Path "c:\temp\today.txt"
PS C:\temp> more today.txt
2022/12/04 13:45:47
2022/12/04 15:21:31     ←実行前の内容(追加されている)

 

 

Add-Contentはファイルに追記していきますが上書きしたい場合はSet-Contentを使用します。



PowerShell

Posted by garnet