|
ここでは、PowerShell's Get-Childitem (gci) のサンプルを紹介します。
- Get-Childitem "C:\"
次の事例を実行してみよう。
# http://www.computerperformance.co.uk/powershell/powershell_file_gci.htm
# PowerShell script to list the files in C: root
Get-Childitem "C:\"
"C:\" から子アイテムを取得し、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
ディレクトリ: C:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2014/04/30 16:14 gs
d----- 2015/12/18 8:09 inetpub
d----- 2015/10/30 14:48 PerfLogs
da---- 2015/12/05 14:03 Perl
d-r--- 2016/07/30 14:25 Program Files
d----- 2016/07/08 12:50 temp
d----- 2016/06/17 10:54 tmp
d-r--- 2015/12/18 9:07 Users
d----- 2014/02/28 19:34 usr
d----- 2016/07/13 20:05 Windows
-a---- 2009/06/11 6:42 24 autoexec.bat
-a---- 2009/06/11 6:42 10 config.sys
-a---- 2014/04/10 17:50 54 GG.txt
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- $Files = gci "C:\Windows\" | Where {$_.extension -eq ".dll"}
次の事例を実行してみよう。
# 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行目では、"C:\Windows\" から子アイテムを取得して、拡張子が ".dll" のものを $Files に格納します。
3行目では、$Files のアイテムを、Name, CreationTime, Length のテーブルに整形して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
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 でも、同様です。
[ 目次 ]
- $Files = gci "C:\Windows\" -Recurse | Where {$_.extension -eq ".dll"}
次の事例を実行してみよう。
# 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行目では、"C:\Windows\" から再帰的に子アイテムを取得して、拡張子が ".dll" のものを $Files に格納します。
5行目では、$Files のアイテムを、Name, CreationTime, Length のテーブルに整形して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
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/07/13 11:54:57 2478592
AcLayers.dll 2016/07/13 11:54:55 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 2016/07/13 11:54:52 88064
ADODB.dll 2015/12/18 8:44:56 110592
EnvDTE.dll 2015/12/18 8:44:58 245760
<省略>
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
管理者権限で実行すること。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- $Files = Gci "C:\Windows\" -Recurse -EA 4 | ? {$_.extension -eq ".dll"}
次の事例を実行してみよう。
# 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: $i
3行目では、"C:\Windows\" から再帰的に子アイテムを取得して、拡張子が ".dll" のものを $Files に格納します。
4から8行で、$Files から1アイテム毎に取り出して、Name, CreationTime, Length を、"{0,-28} {1,-20} {2,12}" の書式で、表示します。
7行目では、アイテムの個数をカウントしています。
それでは、実行してみましょう。下記のような結果が得られます。
<省略>
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: 17173
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
管理者権限で実行すること。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- "{0,-28} {1,-20} {2,12}" が "{0,-48} {1,-20} {2,16}" に変更
次の事例を実行してみよう。
# 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,-48} {1,-20} {2,16}" -f `
$Dll.name, $DLL.CreationTime, $Dll.Length
$i++
}
Write-Host `n The total number of dlls is: $i
この事例は、前項に対して書式 "{0,-28} {1,-20} {2,12}" が "{0,-48} {1,-20} {2,16}" に変更されたものです。
それでは、実行してみましょう。下記のような結果が得られます。
<省略>
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: 17173
桁溢れが無くなっています。
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
管理者権限で実行すること。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Get-Help Get-Childitem -full
次の事例を実行してみよう。
# Research PowerShell Get-Childitem Parameters
Get-Help Get-Childitem -full
# (Try help gci) If you prefer abbreviations.
# (And help gci -full) If you like examples.
Get-Childitem のヘルプを表示します。
それでは、実行してみましょう。下記のような結果が得られます。
" > パイプ問題(?)" 関連で、下記のファイルを差し替えました。
表示内容は、030_PS_GetChildItem_05.txt に保存しました。
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
管理者権限で実行すること。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Get-Childitem | Get-Member
次の事例を実行してみよう。
# Research PowerShell Get-Childitem Properties
Get-Childitem | Get-Member
# (gci | gm) If you enjoy aliases.
# (gci | gm -Membertype property) If want only properties
Get-Childitem のメンバーを表示します。
それでは、実行してみましょう。下記のような結果が得られます。
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.Str...
Create Method void Create(), void Create(System.Security.AccessCon...
CreateObjRef Method System.Runtime.Remoting.ObjRef CreateObjRef(type req...
<省略>
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.Str...
AppendText Method System.IO.StreamWriter AppendText()
CopyTo Method System.IO.FileInfo CopyTo(string destFileName), Syst...
Create Method System.IO.FileStream Create()
<省略>
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- $GciFiles = Get-Childitem C:\ -Force
次の事例を実行してみよう。
# 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.Count
2行目では、C:\ から子アイテムを取得して、$GciFiles に格納します。-Force の修飾子は、隠しアイテムも取得します。
3行目では、$GciFiles を並び変えて、Name, Attributes のテーブルに整形して、表示します。
4行目では、取得したアイテムの個数を表示しています。
それでは、実行してみましょう。下記のような結果が得られます。
<省略>
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: 29
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- $GciFiles = Get-ChildItem "C:\" -Force |where {$_.attributes -Match "System"}
次の事例を実行してみよう。
# 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.Count
2行目では、C:\ から子アイテムを取得して、.attributes が "System" であるものを $GciFiles に格納します。-Force の修飾子は、隠しアイテムも取得します。
3行目では、$GciFiles を並び変えて、Name, Attributes のテーブルに整形して、表示します。
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
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: 13
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- $GciFiles = gci C:\ -Force |where {$_.attributes -ne "Directory"}
次の事例を実行してみよう。
# 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.Count
2行目では、C:\ から子アイテムを取得して、.attributes が "Directory" でないものを $GciFiles に格納します。-Force の修飾子は、隠しアイテムも取得します。
3行目では、$GciFiles を並び変えて、Name, Attributes のテーブルに整形して、表示します。
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.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: 22
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Get-ChildItem | Get-Member -Membertype property
次の事例を実行してみよう。
# PowerShell script to investigate file properties
Get-ChildItem | Get-Member -Membertype property
Get-ChildItem の -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 でも、同様です。
[ 目次 ]
- Get-Alias -Definition Get-ChildItem
次の事例を実行してみよう。
# PowerShell Alias GCI
Get-Alias -Definition Get-ChildItem
Get-ChildItem のエイリアスを取得し、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
CommandType Name Version Source
----------- ---- ------- ------
Alias dir -> Get-ChildItem
Alias gci -> Get-ChildItem
Alias ls -> Get-ChildItem
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Get-Command -Noun Item
次の事例を実行してみよう。
# PowerShell Item Cmdlet Research
Clear-Host
Get-Command -Noun Item
Item の関連コマンドを表示します。
それでは、実行してみましょう。下記のような結果が得られます。
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Clear-Item 3.1.0.0 Microsoft.Power...
Cmdlet Copy-Item 3.1.0.0 Microsoft.Power...
Cmdlet Get-Item 3.1.0.0 Microsoft.Power...
Cmdlet Invoke-Item 3.1.0.0 Microsoft.Power...
Cmdlet Move-Item 3.1.0.0 Microsoft.Power...
Cmdlet New-Item 3.1.0.0 Microsoft.Power...
Cmdlet Remove-Item 3.1.0.0 Microsoft.Power...
Cmdlet Rename-Item 3.1.0.0 Microsoft.Power...
Cmdlet Set-Item 3.1.0.0 Microsoft.Power...
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
|