5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【PowerShell】複数のテキストファイルを結合する(Get-Contentコマンド)

Last updated at Posted at 2022-03-28

環境

  • Microsoft Windows 10.0.22000.318(21H2)
  • PowerShell 7.2.0

準備

PowerShell
# フォルダ「test」を作成
New-Item test -ItemType Directory

# txtファイルを作成
New-Item a.txt -Value a.txtの中身
New-Item b.txt -Value b.txtの中身

# a.txtとb.txtの確認([-Encoding XXX] で文字コードを指定)
Get-Content a.txt, b.txt -Encoding utf8
<# 
a.txtの中身
b.txtの中身
#>
New-Itemコマンドのオプション
New-Item
   [-Path] <String[]>
   [-ItemType <String>]
   [-Value <Object>]
   [-Force]
   [-Credential <PSCredential>]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]

詳しくはこちら

Get-Contentコマンドのオプション
Get-Content
   [-ReadCount <Int64>]
   [-TotalCount <Int64>]
   [-Tail <Int32>]
   [-Path] <String[]>
   [-Filter <String>]
   [-Include <String[]>]
   [-Exclude <String[]>]
   [-Force]
   [-Credential <PSCredential>]
   [-Delimiter <String>]
   [-Wait]
   [-Raw]
   [-Encoding <Encoding>]
   [-AsByteStream]
   [-Stream <String>]
   [<CommonParameters>]

詳しくはこちら

指定したテキストファイルを結合する場合

PowerShell
Get-Content a.txt, b.txt | Set-Content combined.txt

フォルダ内のすべてのテキストファイルを結合する場合

PowerShell
Get-Content *.txt | Set-Content combined.txt

確認

PowerShell
Get-Content combined.txt -Encoding utf8
<# 
a.txtの中身
b.txtの中身
#>

参考

5
6
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
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?