1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Powershell 総ファイルサイズ(byte)取得

Last updated at Posted at 2020-03-18

Powershellでフォルダ配下(サブフォルダを含む)の総ファイルサイズ(byte)を取得する方法です。

結論

dir -r -Force | measure -Property Length -Sum | select Sum

コマンド作成までの経緯

フォルダ配下のファイル情報を取得

dir -r -Force

dirはGet-ChildItemのエイリアス(別名)です。
Get-childItemはフォルダ配下のファイル情報を取得するPowershellの関数です。
そこにオプション-r(ちゃんと書くなら-Recurse)を付与することで、サブフォルダを含むフォルダは以下のファイル情報を取得することができます。

2023/05/31追記
dir -rだと隠しファイルが表示されないため、 隠しファイルも表示するオプションである-Forceを追加しました。
nさんご指摘ありがとうございます。

ファイルサイズだけ指定して合計値を取得する

dir -r -Force | measure -Property Length -Sum

先程のdirコマンドにパイプ(|)を使用して、ファイルサイズだけ取得して合計値を返却する関数を使用します。
measureはmeasure-objectのエイリアスでパイプ以前の関数から返却されたobjectのプロパティを指定して合計値を取得する関数です。
今回はファイルサイズだけ使用したいので、Lengthだけ指定しています。

ファイルサイズの合計が格納されたプロパティを取得する

dir -r -Force | measure -Property Length -Sum | select Sum

measure関数はCount、Average、Sum、Maximum、Minimum、Propertyを返却します。
上記返却値のうち欲しい情報はSumのみなのでselect関数を使用してSumを取得します。
select関数はSelect-Objectのエイリアスで、パイプ以前に返却されたオブジェクトのプロパティを選択して、出力する関数です。

まとめ

以上でフォルダ配下(サブフォルダを含む)の総ファイルサイズを取得するコマンドが完成しました。
ぜひ皆さん使ってみてください。

1
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?