|
|
ファイル操作関連( Get-ChildItem ) |
H.Kamifuji . |
PowerShell では、コマンドプロンプト dir の機能拡張版のようです。Get-ChildItem コマンドを使用しますが、エイリアスとして dir や ls が設定されています。ls とは、UNIX ユーザなら使いなれていると思われます。 MS-DOS そのものが、UNIX のパクりであったと考えるのは、小生だけだろうか? 1970 年代から携わっておられる方々は、理解できると思います。 以下、使用例を説明します。 " > パイプ問題(?)" 関連を、見直し改修しました。 PowerShell 7.15 インストールに共ない見直します。 PowerShell 7.4.6 インストールに共ない見直します。 |
先ずは、ヘルプを見てみよう。# PowerShell Basics: Get-Help for Cmdlets # Clear-Host Get-Help Get-ChildItem -full上記を実行すると下記のような長文が表示されます。 名前 Get-ChildItem 概要 Gets the items and child items in one or more specified locations. #### 省略 ####ヘルプをファイルに保存するには、下記のようなリダイレクションを使用できるようです。PowerShell でのやり方については、別途説明します。 # 出力結果をファイルに保存する。 # Get-Help Get-ChildItem -full > Get-ChildItem_help.txt" > パイプ問題(?)" 関連で、下記のファイルを差し替えました。 出力ファイルは、 Get-ChildItem_help_w.txt ( UTF-8 ) Get-ChildItem_help_w8.txt ( UTF-8 BOM 付き ) Get-ChildItem_help_s.txt ( Shift_JIS ) です。 上記を実行すると PowerShell 7.15 でも、正常(同様)に、実行されます。 PowerShell 7.4.6 でも、同様です。 PowerShell 7.x.x 以降は、Get-Help の表示が英語のままです。ローカライズができないようです。 日本語で見たい時には、PowerShell 5.X.X を立ち上げ実行してください。それなりの情報が得られるでしょう。 Windows10 の場合は、powershell コマンドで、5.x.x に切り替えできます。 |
基本的な事例です。c:\windows 中の拡張子が .log のファイルを検索します。# Basic PowerShell example to find Windows log files Get-Childitem C:\Windows\*.log上記を実行すると、下記のように表示されます。 ディレクトリ: C:\Windows Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 2015/12/18 10:48 13196 comsetup.log -a---- 2015/12/18 10:32 3949 DtcInstall.log -a---- 2015/07/26 15:26 324632 hpdj4500.log -a---- 2015/07/30 15:46 237820 hpDJZ6200.log -a---- 2014/02/28 15:33 12902 IE10_main.log -a---- 2014/02/28 17:25 8591 IE11_main.log -a---- 2014/02/28 15:49 4072 IE9_main.log -a---- 2015/12/18 8:44 27408 iis.log -a---- 2016/06/20 14:25 5324 PFRO.log -a---- 2016/06/16 6:55 32592 setupact.log -a---- 2015/12/18 8:29 0 setuperr.log -a---- 2014/02/28 11:34 1355 TSSysprep.log -a---- 2016/07/07 11:43 275 WindowsUpdate.log上記の Mode 列は、UNIX に携わっている方は、見慣れている考えるが、各アイテムの属性が表示されます。 ただし、意味は、UNIX と若干異なっています。下記の6文字で表現されます。非該当部は - で表示されます。
上記を実行すると PowerShell 7.15 でも、正常(同様)に、実行されます。 PowerShell 7.4.6 でも、同様です。 |
下記を実行してみます。# PowerShell example to find Windows log files # Clear-Host $Directory = "C:\Windows\" $Files = Get-Childitem $Directory -recurse -Include *.log -ErrorAction SilentlyContinue $Files 少しコードを説明しておきます。3行目は、探査開始ディレクトリを $Directory に格納しています。4行目で Get-ChildItem のパラメータとして利用しています。 -recurse は、再帰的にディレクトリを探査することを指定しています。 -Include *.log は、正規表現を利用して、拡張子 .log のファイルを検索することを指定しています。 -ErrorAction SilentlyContinue は、途中、読めないディレクトリに遭遇したときなどのエラーメッセージを出力しないで続行することを指定しています。 実行結果は、下記です。長文のため冒頭部だけにしました。 ディレクトリ: C:\Windows\assembly\GAC_MSIL Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 2015/12/18 8:09 System.IO.Log ディレクトリ: C:\Windows\debug\WIA Mode LastWriteTime Length Name ---- ------------- ------ ---- 2016/07/11 11:24 1276778 wiatrace.log #### 省略 ####上記を実行すると PowerShell 7.15 でも、正常(同様)に、実行されます。 PowerShell 7.4.6 でも、同様です。 |
次の事例を実行してみます。# PowerShell example to find and sort Windows log files # Clear-Host $Directory = "C:\Windows\" $Phrase = "Error" $Files = Get-Childitem $Directory -recurse -Include *.log -ErrorAction SilentlyContinue $Files | Select-String $Phrase -ErrorAction SilentlyContinue | Group-Object filename | Sort-Object count -descending 少しコードを説明しておきます。5行目までは、前項と同じです。 6行目の Select-String $Phrase は、部分文字列が存在する行を選択します。-ErrorAction SilentlyContinue は、前項で説明しました。 Group-Object filename は、filename グルーピングして、個数を計測します。 Sort-Object count は、count で並び換えて、出力することを指定しています。-descending は、降順に並び変えることを指定しています。 実行結果は、下記です。長文のため冒頭部だけにしました。 Count Name Group ----- ---- ----- 3746 setupact.log {C:\Windows\Logs\DPX\setupact.log:1:2015-12-18 11:23:44, Info ... 482 setupapi.dev.20150804_... {C:\Windows\INF\setupapi.dev.20150804_203539.log:388: inf: ... 431 setupapi.offline.20151... {C:\Windows\INF\setupapi.offline.20151029_224841.log:53: sto: U... 119 wiatrace.log {C:\Windows\debug\WIA\wiatrace.log:39:WIA: 2328.2500 15 8 0 [wiaser... 99 hpDJZ6200.log {C:\Windows\hpDJZ6200.log:359:CMSIObj::_openMSISource, ValidateFile... 87 hpdj4500.log {C:\Windows\hpdj4500.log:363:CDataFileReader::_isValidReader, Reade... 79 setuperr.log {C:\Windows\Logs\SetupCleanupTask\setuperr.log:1:2016-01-21 16:20:3... 51 setupapi.dev.log {C:\Windows\INF\setupapi.dev.log:193: inf: ... #### 省略 #### 上記を実行すると PowerShell 7.15 でも、正常(同様)に、実行されます。 PowerShell 7.4.6 でも、同様です。 |
次の事例を実行してみます。# PowerShell gci example to list .dll in Windows folder $Files = gci "C:\Windows\" | Where {$_.extension -eq ".dll"} $Files | Format-Table Name, CreationTime, Length -auto 少しコードを説明しておきます。2行目は、前項と同じく拡張子が .dll のファイルを検索します。gci は、Get-ChildItem のエイリアスです。Where {$_.extension -eq ".dll"} は、拡張子が .dll であるとき検索することを指定します。where の使い方は、別途説明します。 3行目は、出力結果をテーブルに成形します。メンバーは、Name, CreationTime, Length を指定し、-auto は、表示桁数を自動で選択されることを指定しています。 実行結果は、下記です。 Name CreationTime Length ---- ------------ ------ twain.dll 2015/10/30 14:45:46 94784 twain_32.dll 2015/10/30 14:45:46 60416上記を実行すると PowerShell 7.15 でも、正常(同様)に、実行されます。 PowerShell 7.4.6 でも、同様です。 |
次の事例を実行してみます。# PowerShell example to list .dll in Windows folder and sub-folders Write-Host `n "Waiting ...." $Files = gci "C:\Windows\" -Recurse | Where {$_.extension -eq ".dll"} Clear-Host $Files | Format-Table Name, CreationTime, Length -auto 処理内容は、前項と同じです。ただし、3行目に -Recurse が追加されて、再帰的に多くのファイルがけんさくされます。 中には、長いファイル名もあり、PowerShell 画面の幅に合わせ拡張されています。 実行結果は、下記です。 Name CreationTime Length ---- ------------ ------ twain.dll 2015/10/30 14:45:46 94784 twain_32.dll 2015/10/30 14:45:46 60416 AcGenral.dll 2016/04/13 10:32:42 2478592 AcLayers.dll 2015/10/30 14:45:10 335360 AcRes.dll 2015/12/18 8:15:07 320352 AcSpecfc.dll 2016/06/15 14:47:09 464896 AcWinRT.dll 2016/04/13 10:32:09 31744 AcXtrnal.dll 2015/10/30 14:45:10 88064 ADODB.dll 2015/12/18 8:44:56 110592 EnvDTE.dll 2015/12/18 8:44:58 245760 EnvDTE80.dll 2015/12/18 8:44:59 135168 EnvDTE90.dll 2015/12/18 8:44:59 18944 EnvDTE90a.dll 2015/12/18 8:44:59 12800 Extensibility.dll 2015/12/18 8:44:59 4608 Microsoft.DirectX.dll 2015/12/18 8:46:19 223232 Microsoft.DirectX.AudioVideoPlayback.dll 2015/12/18 8:46:19 53248 Microsoft.DirectX.Diagnostics.dll 2015/12/18 8:46:19 12800 #### 省略 #### 次項で、表示桁数を指定して、テーブルに成型するサンプルを説明します。 上記を実行すると PowerShell 7.15 でも、正常(同様)に、実行されます。 PowerShell 7.4.6 でも、同様です。 |
次の事例を実行してみます。# PowerShell script to list the dll files under C:\Windows\System32 $i =0 $Files = Gci "C:\Windows\" -Recurse -EA 4 | ? {$_.extension -eq ".dll"} Foreach ($Dll in $Files) { "{0,-28} {1,-20} {2,12}" -f $Dll.name, $DLL.CreationTime, $Dll.Length $i++ } Write-Host `n The total number of dlls is: $i3行目までは、前項と同じ処理です。 4行目から7行目までは、Foreach 文による反復処理で、テーブルに成形しています。($Dll in $Files) は、$Files から1レコードのデータを $Dll に取り込みます。 5行目で、桁数指定してテーブルに成形します。[0,-28} は、0番目のメンバーを左詰め28桁で、{1,-20} は、1番目のメンバーを左詰め20桁で、{2,12} は、2番目のメンバーを右詰め12桁で、表示することを指定しています。 6行目では、検索されたファイルの個数を計測しています。 実行結果は、下記です。 #### 省略 #### ReachFramework.dll 2016/04/13 10:32:02 532480 System.Speech.dll 2015/12/18 8:08:56 692224 UIAutomationClientsideProviders.dll 2015/12/18 8:08:56 380928 UIAutomationClient.dll 2015/12/18 8:08:56 172032 UIAutomationProvider.dll 2015/12/18 8:08:56 40960 UIAutomationTypes.dll 2015/12/18 8:08:56 98304 WindowsBase.dll 2015/12/18 8:08:56 9117 WindowsBase.dll 2016/03/10 7:51:35 3549 WindowsBase.dll 2016/04/13 10:32:40 1253376 WindowsFormsIntegration.dll 2015/12/18 8:08:56 94208 WSDApi.dll 2015/10/30 14:44:43 29455 WSDApi.dll 2016/04/13 10:32:13 564224 WSDScDrv.dll 2015/10/30 14:44:28 246784 WUDFUsbccidDriver.dll 2015/10/30 14:44:26 276 WUDFUsbccidDriver.dll 2016/05/11 14:38:52 76288 vmbuspipe.dll 2015/10/30 14:44:25 23776 System.Workflow.Activities.dll 2015/12/18 8:08:56 1142784 System.Workflow.ComponentModel.dll 2015/12/18 8:08:56 1630208 System.Workflow.Runtime.dll 2015/12/18 8:08:57 540672 The total number of dlls is: 16832上記は、最終部を示していますが、168332 個もあるとは、びっくりです。 しかし、指定した桁数に収まらない時には、右側にシフトされるようです。 上記を実行すると PowerShell 7.15 でも、正常(同様)に、実行されます。 PowerShell 7.4.6 でも、同様です。 |
次の事例を実行してみます。# List ALL files in the root $GciFiles = Get-Childitem C:\ -Force $GciFiles | Sort-Object | FT Name, Attributes -auto Write-Host "Number of files in the root: " $GciFiles.Count2行目の -Force は、 隠し、システム、読取専用などすべてのアイテムを検索することを指定しています。 3行目は、前項らと同じくテーブルに成形します。 4行目の $GciFiles.Count には、アイテムの個数が格納されています。 実行結果は、下記です。 Name Attributes ---- ---------- $Recycle.Bin Hidden, System, Directory $WINRE_BACKUP_PARTITION.MARKER Hidden, Archive autoexec.bat Archive Boot Hidden, System, Directory bootmgr ReadOnly, Hidden, System, Archive BOOTNXT Hidden, System, Archive BOOTSECT.BAK ReadOnly, Hidden, System, Archive Config.Msi Hidden, System, Directory config.sys Archive Documents and Settings Hidden, System, Directory, ReparsePoint, NotContentIndexed GG.txt Archive gs Directory hiberfil.sys Hidden, System, Archive, NotContentIndexed inetpub Directory IO.SYS ReadOnly, Hidden, System, Archive MSDOS.SYS ReadOnly, Hidden, System, Archive MSOCache ReadOnly, Hidden, Directory, NotContentIndexed pagefile.sys Hidden, System, Archive PerfLogs Directory Perl Directory, Archive Program Files ReadOnly, Directory ProgramData Hidden, Directory Recovery Hidden, System, Directory, NotContentIndexed swapfile.sys Hidden, System, Archive System Volume Information Hidden, System, Directory temp Directory tmp Directory Users ReadOnly, Directory usr Directory Windows Directory Number of files in the root: 30 2行目の -Force を指定しないと、下記のような結果になります。 Name Attributes ---- ---------- autoexec.bat Archive config.sys Archive GG.txt Archive gs Directory inetpub Directory PerfLogs Directory Perl Directory, Archive Program Files ReadOnly, Directory temp Directory tmp Directory Users ReadOnly, Directory usr Directory Windows Directory Number of files in the root: 13 上記を実行すると PowerShell 7.15 でも、正常(同様)に、実行されます。 PowerShell 7.4.6 でも、同様です。 |
次の事例を実行してみます。# PowerShell cmdlet to list JUST the System files in the root of C:\ $GciFiles = Get-ChildItem "C:\" -Force | where {$_.attributes -Match "System"} $GciFiles | Sort-Object | Format-Table name, attributes -auto Write-Host "Number of system files: " $GciFiles.Count2行目は、前項のコードに対して、 | where {$_.attributes -Match "System"} が追加され、システムアイテムのみ選択します。 3〜4行目は、前項と同じです。 実行結果は、下記です。 Name Attributes ---- ---------- $Recycle.Bin Hidden, System, Directory Boot Hidden, System, Directory bootmgr ReadOnly, Hidden, System, Archive BOOTNXT Hidden, System, Archive BOOTSECT.BAK ReadOnly, Hidden, System, Archive Config.Msi Hidden, System, Directory Documents and Settings Hidden, System, Directory, ReparsePoint, NotContentIndexed hiberfil.sys Hidden, System, Archive, NotContentIndexed IO.SYS ReadOnly, Hidden, System, Archive MSDOS.SYS ReadOnly, Hidden, System, Archive pagefile.sys Hidden, System, Archive Recovery Hidden, System, Directory, NotContentIndexed swapfile.sys Hidden, System, Archive System Volume Information Hidden, System, Directory Number of system files: 14 上記を実行すると PowerShell 7.15 でも、正常(同様)に、実行されます。 PowerShell 7.4.6 でも、同様です。 |
次の事例を実行してみます。# PowerShell cmdlet to list the System files in the root of C:\ $GciFiles = gci C:\ -Force | where {$_.attributes -ne "Directory"} $GciFiles | Sort-Object | FT Name, Attributes -auto Write-Host "Number of plain files: " $GciFiles.Count2行目は、前項のコードに対して、 | where {$_.attributes -Match "System"} が | where {$_.attributes -ne "Directory"} 変更され、ディレクトリ以外を選択します。 3〜4行目は、前項と同じです。 実行結果は、下記です。 Name Attributes ---- ---------- $Recycle.Bin Hidden, System, Directory $WINRE_BACKUP_PARTITION.MARKER Hidden, Archive autoexec.bat Archive Boot Hidden, System, Directory bootmgr ReadOnly, Hidden, System, Archive BOOTNXT Hidden, System, Archive BOOTSECT.BAK ReadOnly, Hidden, System, Archive Config.Msi Hidden, System, Directory config.sys Archive Documents and Settings Hidden, System, Directory, ReparsePoint, NotContentIndexed GG.txt Archive hiberfil.sys Hidden, System, Archive, NotContentIndexed IO.SYS ReadOnly, Hidden, System, Archive MSDOS.SYS ReadOnly, Hidden, System, Archive MSOCache ReadOnly, Hidden, Directory, NotContentIndexed pagefile.sys Hidden, System, Archive Perl Directory, Archive Program Files ReadOnly, Directory ProgramData Hidden, Directory Recovery Hidden, System, Directory, NotContentIndexed swapfile.sys Hidden, System, Archive System Volume Information Hidden, System, Directory Users ReadOnly, Directory Number of plain files: 23 上記を実行すると PowerShell 7.15 でも、正常(同様)に、実行されます。 PowerShell 7.4.6 でも、同様です。 |
次の事例を実行してみます。# Research PowerShell Get-Childitem Properties Get-Childitem | Get-Member実行結果は、下記に示します。長文なので途中省略しました。各自確認してみてください。 TypeName: System.IO.DirectoryInfo Name MemberType Definition ---- ---------- ---------- LinkType CodeProperty System.String LinkType{get=GetLinkType;} Mode CodeProperty System.String Mode{get=Mode;} Target CodeProperty System.Collections.Generic.IEnumerable`1[[System.String, mscorlib, Version=... Create Method void Create(), void Create(System.Security.AccessControl.DirectorySecurity ... CreateObjRef Method System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType) CreateSubdirectory Method System.IO.DirectoryInfo CreateSubdirectory(string path), System.IO.Director... Delete Method void Delete(), void Delete(bool recursive) #### 省略 #### LastWriteTime Property datetime LastWriteTime {get;set;} LastWriteTimeUtc Property datetime LastWriteTimeUtc {get;set;} Name Property string Name {get;} Parent Property System.IO.DirectoryInfo Parent {get;} Root Property System.IO.DirectoryInfo Root {get;} BaseName ScriptProperty System.Object BaseName {get=$this.Name;} TypeName: System.IO.FileInfo Name MemberType Definition ---- ---------- ---------- LinkType CodeProperty System.String LinkType{get=GetLinkType;} Mode CodeProperty System.String Mode{get=Mode;} Target CodeProperty System.Collections.Generic.IEnumerable`1[[System.String, mscorlib, Version=... AppendText Method System.IO.StreamWriter AppendText() CopyTo Method System.IO.FileInfo CopyTo(string destFileName), System.IO.FileInfo CopyTo(s... Create Method System.IO.FileStream Create() CreateObjRef Method System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType) #### 省略 #### LastWriteTime Property datetime LastWriteTime {get;set;} LastWriteTimeUtc Property datetime LastWriteTimeUtc {get;set;} Length Property long Length {get;} Name Property string Name {get;} BaseName ScriptProperty System.Object BaseName {get=if ($this.Extension.Length -gt 0){$this.Name.Re... VersionInfo ScriptProperty System.Object VersionInfo {get=[System.Diagnostics.FileVersionInfo]::GetVer... 上記を実行すると PowerShell 7.15 でも、正常(同様)に、実行されます。 PowerShell 7.4.6 でも、同様です。 |
次の事例を実行してみます。# PowerShell script to investigate file properties Get-ChildItem | Get-Member -Membertype property実行結果は、下記です。 TypeName: System.IO.DirectoryInfo Name MemberType Definition ---- ---------- ---------- Attributes Property System.IO.FileAttributes Attributes {get;set;} CreationTime Property datetime CreationTime {get;set;} CreationTimeUtc Property datetime CreationTimeUtc {get;set;} Exists Property bool Exists {get;} Extension Property string Extension {get;} FullName Property string FullName {get;} LastAccessTime Property datetime LastAccessTime {get;set;} LastAccessTimeUtc Property datetime LastAccessTimeUtc {get;set;} LastWriteTime Property datetime LastWriteTime {get;set;} LastWriteTimeUtc Property datetime LastWriteTimeUtc {get;set;} Name Property string Name {get;} Parent Property System.IO.DirectoryInfo Parent {get;} Root Property System.IO.DirectoryInfo Root {get;} TypeName: System.IO.FileInfo Name MemberType Definition ---- ---------- ---------- Attributes Property System.IO.FileAttributes Attributes {get;set;} CreationTime Property datetime CreationTime {get;set;} CreationTimeUtc Property datetime CreationTimeUtc {get;set;} Directory Property System.IO.DirectoryInfo Directory {get;} DirectoryName Property string DirectoryName {get;} Exists Property bool Exists {get;} Extension Property string Extension {get;} FullName Property string FullName {get;} IsReadOnly Property bool IsReadOnly {get;set;} LastAccessTime Property datetime LastAccessTime {get;set;} LastAccessTimeUtc Property datetime LastAccessTimeUtc {get;set;} LastWriteTime Property datetime LastWriteTime {get;set;} LastWriteTimeUtc Property datetime LastWriteTimeUtc {get;set;} Length Property long Length {get;} Name Property string Name {get;} 上記を実行すると PowerShell 7.15 でも、正常(同様)に、実行されます。 PowerShell 7.4.6 でも、同様です。 |
次の事例を実行してみます。# PowerShell Alias GCI Get-Alias -Definition Get-ChildItem実行結果は、下記です。 CommandType Name Version Source ----------- ---- ------- ------ Alias dir -> Get-ChildItem Alias gci -> Get-ChildItem Alias ls -> Get-ChildItemUNIX を意識した ls は、評価するが、ls は、アイテム名だけを羅列します。ls -l で、よく似た形式で出力されます。 やはり、訳の分からない UNIX を知らない開発者しか居なくなったのでしょう。 上記を実行すると PowerShell 7.15 でも、正常(同様)に、実行されます。 |
次の事例を実行してみます。# PowerShell Item Cmdlet Research Get-Command -Noun Item実行結果は、下記です。 CommandType Name Version Source ----------- ---- ------- ------ Cmdlet Clear-Item 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Copy-Item 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Get-Item 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Invoke-Item 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Move-Item 3.1.0.0 Microsoft.PowerShell.Management Cmdlet New-Item 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Remove-Item 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Rename-Item 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Set-Item 3.1.0.0 Microsoft.PowerShell.Management 上記を実行すると PowerShell 7.15 でも、正常(同様)に、実行されます。 PowerShell 7.4.6 でも、同様です。 |