|
ここでは、PowerShell Scripting -Recurse のサンプルを紹介します。
- Get-ChildItem -path "C:\Program Files\"
次の事例を実行してみよう。
# http://www.computerperformance.co.uk/powershell/powershell_recurse.htm
# PowerShell With Just Get-ChildItem (no recurse)
Clear-Host
Get-ChildItem -path "C:\Program Files\"
"C:\Program Files\" の子アイテムを取得して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
ディレクトリ: C:\Program Files
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2016/06/17 20:03 Adobe
d----- 2015/07/16 9:43 Android
d----- 2016/05/06 13:00 AppInsights
da---- 2016/03/14 18:31 Apple Software Update
da---- 2016/05/06 13:05 Application Verifier
d----- 2015/08/06 10:03 Baidu
da---- 2015/09/18 8:55 Bonjour
d----- 2016/07/13 20:05 CMAK
d----- 2016/07/21 8:42 Common Files
d----- 2016/05/06 12:04 Common7
< 省略 >
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Get-ChildItem -path "C:\Program Files\" -Recurse
次の事例を実行してみよう。
# PowerShell -Recurse parameter
Clear-Host
Get-ChildItem -path "C:\Program Files\" -Recurse
"C:\Program Files\" から、再帰的に子アイテムを取得して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
< 省略 >
ディレクトリ: C:\Program Files\Adobe
Mode LastWriteTime Length Name
---- ------------- ------ ----
da---- 2016/07/13 7:29 Acrobat Reader DC
ディレクトリ: C:\Program Files\Adobe\Acrobat Reader DC
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2016/07/13 7:29 Esl
d----- 2016/08/04 12:55 Reader
d----- 2016/06/17 20:03 Resource
d----- 2016/06/17 20:03 Setup Files
-a---- 2015/03/17 1:34 16633 ReadMe.htm
-a---- 2015/03/17 1:34 17153 ReadMeJ.htm
ディレクトリ: C:\Program Files\Adobe\Acrobat Reader DC\Esl
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2016/06/30 20:55 322232 AiodLite.dll
< 省略 >
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Get-ChildItem -path $Directory -Recurse
次の事例を実行してみよう。
# PowerShell -Recurse parameter
Clear-Host
$Directory = "C:\Program Files\Windows*"
Get-ChildItem -path $Directory -Recurse
"C:\Program Files\Windows*" から、再帰的に子アイテムを取得して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
ディレクトリ: C:\Program Files\Windows Defender
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2015/10/31 0:00 ja-JP
ディレクトリ: C:\Program Files\Windows Defender\ja-JP
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---l 2015/10/30 23:59 3072 EppManifest.dll.mui
-a---l 2015/10/30 23:59 28160 MpAsDesc.dll.mui
-a---l 2015/10/30 23:59 23040 MpEvMsg.dll.mui
-a---l 2015/10/30 23:59 56832 MsMpRes.dll.mui
-a---l 2015/10/30 23:59 21504 ProtectionManagement.dll.mui
-a---l 2015/10/30 23:59 62062 ProtectionManagement.mfl
-a---l 2015/10/30 23:59 930 ProtectionManagement_Uninstall.mfl
-a---l 2015/10/30 23:59 3584 shellext.dll.mui
ディレクトリ: C:\Program Files\Windows Defender
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---l 2015/10/30 14:44 9398 AmMonitoringInstall.mof
-a---l 2015/10/30 14:44 142336 AMMonitoringProvider.dll
-a---l 2015/10/30 14:44 21004 AmStatusInstall.mof
-a---l 2015/10/30 14:44 2460 ClientWMIInstall.mof
-a---l 2015/10/30 14:44 231936 ConfigSecurityPolicy.exe
< 省略 >
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Get-ChildItem -path $Directory -Recurse -Include *.exe
次の事例を実行してみよう。
# PowerShell -Recurse parameter
Clear-Host
$Directory = "C:\Program Files\Windows*"
Get-ChildItem -path $Directory -Recurse -Include *.exe
"C:\Program Files\Windows*" から、再帰的に子アイテムを取得して、拡張子が .exe のものを表示します。
それでは、実行してみましょう。下記のような結果が得られます。
ディレクトリ: C:\Program Files\Windows Defender
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---l 2015/10/30 14:44 231936 ConfigSecurityPolicy.exe
-a---l 2016/07/01 13:19 310496 MpCmdRun.exe
-a---l 2015/10/30 14:44 47456 MpUXSrv.exe
-a---l 2016/07/01 12:25 994304 MSASCui.exe
-a---l 2016/07/01 13:19 23264 MsMpEng.exe
-a---l 2015/10/30 14:44 280376 NisSrv.exe
ディレクトリ: C:\Program Files\Windows Identity Foundation\v3.5
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---l 2015/12/18 8:09 5632 c2wtshost.exe
ディレクトリ: C:\Program Files\Windows Journal
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---l 2015/10/31 0:03 1883136 Journal.exe
-a---l 2015/10/31 0:03 48128 PDIALOG.exe
< 省略 >
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- | Sort-Object Name | Format-Table Name, Fullname -auto
次の事例を実行してみよう。
# PowerShell -Recurse parameter
Clear-Host
$Directory = "C:\Program Files\Windows*"
Get-ChildItem -path $Directory -Recurse -Include *.exe `
| Sort-Object Name | Format-Table Name, Fullname -auto
"C:\Program Files\Windows*" から、再帰的に拡張子が .exe の子アイテムを取得して、Name で並び変えて、Name, Fullname のてブルに整形して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
Name FullName
---- --------
AccCheckConsole.exe C:\Program Files\Windows Kits\8.1\bin\x64\AccChecker\AccC...
AccCheckConsole.exe C:\Program Files\Windows Kits\8.0\bin\x86\AccChecker\AccC...
AccCheckConsole.exe C:\Program Files\Windows Kits\10\bin\arm\AccChecker\AccCh...
AccCheckConsole.exe C:\Program Files\Windows Kits\10\bin\x86\AccChecker\AccCh...
AccCheckConsole.exe C:\Program Files\Windows Kits\8.0\bin\x64\AccChecker\AccC...
AccCheckConsole.exe C:\Program Files\Windows Kits\8.1\bin\x86\AccChecker\AccC...
AccCheckConsole.exe C:\Program Files\Windows Kits\8.0\bin\arm\AccChecker\AccC...
AccCheckConsole.exe C:\Program Files\Windows Kits\10\bin\x64\AccChecker\AccCh...
AccCheckConsole.exe C:\Program Files\Windows Kits\10\bin\arm64\AccChecker\Acc...
AccCheckConsole.exe C:\Program Files\Windows Kits\8.1\bin\arm\AccChecker\AccC...
acccheckui.exe C:\Program Files\Windows Kits\8.0\bin\arm\AccChecker\accc...
acccheckui.exe C:\Program Files\Windows Kits\8.1\bin\x86\AccChecker\accc...
acccheckui.exe C:\Program Files\Windows Kits\8.0\bin\x64\AccChecker\accc...
acccheckui.exe C:\Program Files\Windows Kits\8.0\bin\x86\AccChecker\accc...
< 省略 >
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Get-ChildItem "HKLM:\Software\Microsoft\PowerShell" -Recurse
次の事例を実行してみよう。
# PowerShell Script To Search The Registry
Clear-Host
$Path = "HKLM:\Software\Microsoft\PowerShell"
Get-ChildItem $Path -Recurse
"HKLM:\Software\Microsoft\PowerShell" から、再帰的に子アイテム(レジストリ)を取得して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
Hive: HKEY_LOCAL_MACHINE\Software\Microsoft\PowerShell
Name Property
---- --------
1 Install : 1
PID : 89383-100-0001260-04309
Hive: HKEY_LOCAL_MACHINE\Software\Microsoft\PowerShell\1
Name Property
---- --------
0409 Install : 1
0411 Install : 1
PowerShellEngine ApplicationBase : C:\Windows\System32\WindowsPowerShell\v1.0
ConsoleHostAssemblyName : Microsoft.PowerShell.ConsoleHost, Version=1.0.0.0,
Culture=neutral, PublicKeyToken
=31bf3856ad364e35, ProcessorArchitecture=msil
ConsoleHostModuleName : C:\Windows\System32\WindowsPowerShell\v1.0\Microsof
t.PowerShell.ConsoleHost.dll
PowerShellVersion : 2.0
PSCompatibleVersion : 1.0, 2.0
RuntimeVersion : v2.0.50727
PowerShellSnapIns
< 省略 >
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Get-Childitem $Path -Recurse | where {$_.Extension -Match "exe"}
次の事例を実行してみよう。
# PowerShell script to find executable in the Windows folder
Clear-Host
$Path = "C:\Windows\System32"
Get-Childitem $Path -Recurse | where {$_.Extension -Match "exe"}`
| ft -group {$_.Path} Directory, Name -autosize
"C:\Windows\System32" から、再帰的に拡張子が "exe" の子アイテムを取得して、.Path でグルーピングして、Directory, Name のテーブルに整形して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
:
Directory Name
--------- ----
C:\Windows\System32 acu.exe
C:\Windows\System32 aitstatic.exe
C:\Windows\System32 alg.exe
C:\Windows\System32 append.exe
C:\Windows\System32 appidcertstorech...
C:\Windows\System32 appidpolicyconve...
C:\Windows\System32 ApplicationFrame...
C:\Windows\System32 appverif.exe
C:\Windows\System32 ARP.EXE
C:\Windows\System32 at.exe
< 省略 >
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Get-Childitem $Path -Recurse -ErrorAction SilentlyContinue
次の事例を実行してみよう。
# PowerShell script to find executables in the Windows\System32 folder
Clear-Host
$Path = "C:\Windows\System32"
Get-Childitem $Path -Recurse -ErrorAction SilentlyContinue |`
where {$_.Extension -Match "exe"} | ft -group {$_.Path} Directory, Name -auto
"C:\Windows\System32" から、再帰的に拡張子が "exe" の子アイテムを取得して、.Path でグルーピングして、Directory, Name のテーブルに整形して、表示します。
処理内容は、前項と同じですが、-ErrorAction SilentlyContinue が追加されています。エラーが発生したときに、メッセージを表示しないで、継続します。
それでは、実行してみましょう。下記のような結果が得られます。
:
Directory Name
--------- ----
C:\Windows\System32 acu.exe
C:\Windows\System32 aitstatic.exe
C:\Windows\System32 alg.exe
C:\Windows\System32 append.exe
C:\Windows\System32 appidcertstorech...
C:\Windows\System32 appidpolicyconve...
C:\Windows\System32 ApplicationFrame...
C:\Windows\System32 appverif.exe
C:\Windows\System32 ARP.EXE
C:\Windows\System32 at.exe
< 省略 >
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- $WinExe = Get-ChildItem $Path -Recurse ; $WinExe.count
次の事例を実行してみよう。
# PowerShell recurse finds executables under the Windows folder
Clear-Host
$Path = "C:\Windows\*.exe"
$WinExe = Get-ChildItem $Path -Recurse
$WinExe.count
"C:\Windows\*.exe" から、再帰的に *.exe の子アイテムを取得して、個数を表示ます。
それでは、実行してみましょう。下記のような結果が得られます。
2031
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- $WinExe = Get-Childitem $Path -Recurse -ErrorAction SilentlyContinue | Where-Object {$_.Extension -Match "exe"}
次の事例を実行してみよう。
# PowerShell script to find ALL executables under Windows folder
Write-Host "Waiting for -Recurse ..."
$Path = "C:\Windows\"
$WinExe = Get-Childitem $Path -Recurse -ErrorAction SilentlyContinue `
| Where-Object {$_.Extension -Match "exe"}
Clear-Host
$WinExe.count
"C:\Windows\*.exe" から、再帰的に *.exe の子アイテムを取得して、個数を表示ます。
それでは、実行してみましょう。下記のような結果が得られます。
2063
前項と微妙に個数が異なるのは、何故か?
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Get-Command -CommandType cmdlet | where { $_.parameters.keys -Contains "recurse"}
次の事例を実行してみよう。
Clear-Host
Get-Command -CommandType cmdlet `
| where { $_.parameters.keys -Contains "recurse"}
-CommandType が cmdlet で、.parameters.keys に "recurse" が含まれるコマンドを取得して、表示します。
それでは、実行してみましょう。下記のような結果が得られます。
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Copy-Item 3.1.0.0 Microsoft.PowerShell.Manage...
Cmdlet Get-ChildItem 3.1.0.0 Microsoft.PowerShell.Manage...
Cmdlet Get-WmiObject 3.1.0.0 Microsoft.PowerShell.Manage...
Cmdlet Remove-Item 3.1.0.0 Microsoft.PowerShell.Manage...
Cmdlet Update-Help 3.0.0.0 Microsoft.PowerShell.Core
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- Get-Help Get-Childitem
次の事例を実行してみよう。
Clear-Host
Get-Help Get-Childitem
Get-Childitem のヘルプを表示します。
それでは、実行してみましょう。下記のような結果が得られます。
" > パイプ問題(?)" 関連で、下記のファイルを差し替えました。
表示内容は、062_PS_Recurse_12.txt に保存しました。
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
ただし、英語です。
[ 目次 ]
- foreach ($file in $List) {$file.Path; $i++}
次の事例を実行してみよう。
Clear-Host
$i=0
$Path = "C:\Windows"
$Full = Get-ChildItem $Path -Include *.txt -Recurse
$StringText = "Microsoft"
$List = select-string -pattern $StringText $Full
foreach ($file in $List) {$file.Path; $i++}
$i
3〜4行で、"C:\Windows" から、再帰的に拡張子が .txt の子アイテムを取得して、$Full に格納します。
5〜6行で、$Full から、"Microsoft" のパターンがあるものを、$List に格納します。
7行目で、$List から、アイテム毎に $file.Path を表示するのと、個数をカウントします。
8行目で、個数を表示しています。
それでは、実行してみましょう。下記のような結果が得られます。
< 省略 >
C:\Windows\WinSxS\x86_multimedia-generalmididata_31bf3856ad364e35_10.0.10586.0_none_b92448a0485cd55f\gmreadme.txt
C:\Windows\WinSxS\x86_multimedia-generalmididata_31bf3856ad364e35_10.0.10586.0_none_b92448a0485cd55f\gmreadme.txt
C:\Windows\WinSxS\x86_multimedia-generalmididata_31bf3856ad364e35_10.0.10586.0_none_b92448a0485cd55f\gmreadme.txt
C:\Windows\WinSxS\x86_multimedia-generalmididata_31bf3856ad364e35_10.0.10586.0_none_b92448a0485cd55f\gmreadme.txt
C:\Windows\WinSxS\x86_netfx4-attributionfile_b03f5f7f11d50a3a_4.0.10586.0_none_2a3e901af0c2891b\ThirdPartyNotices.txt
C:\Windows\WinSxS\x86_netfx4-attributionfile_b03f5f7f11d50a3a_4.0.10586.0_none_2a3e901af0c2891b\ThirdPartyNotices.txt
526
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
[ 目次 ]
- foreach ($file in $List) {$file.Path; $i++}
次の事例を実行してみよう。
$i=0
$Full = Get-ChildItem C:\windows -Include *.txt -Recurse
$List = select-string -pattern "Microsoft" $Full
foreach ($file in $List) {$file.Path; $i++}
$i
この事例は、前項と同じ処理を実行します。変数 $Path や $StringText を使用しないで、直にコマンド中に指定しています。
それでは、実行してみましょう。下記のような結果が得られます。
< 省略 >
C:\Windows\WinSxS\x86_multimedia-generalmididata_31bf3856ad364e35_10.0.10586.0_none_b92448a0485cd55f\gmreadme.txt
C:\Windows\WinSxS\x86_multimedia-generalmididata_31bf3856ad364e35_10.0.10586.0_none_b92448a0485cd55f\gmreadme.txt
C:\Windows\WinSxS\x86_multimedia-generalmididata_31bf3856ad364e35_10.0.10586.0_none_b92448a0485cd55f\gmreadme.txt
C:\Windows\WinSxS\x86_multimedia-generalmididata_31bf3856ad364e35_10.0.10586.0_none_b92448a0485cd55f\gmreadme.txt
C:\Windows\WinSxS\x86_netfx4-attributionfile_b03f5f7f11d50a3a_4.0.10586.0_none_2a3e901af0c2891b\ThirdPartyNotices.txt
C:\Windows\WinSxS\x86_netfx4-attributionfile_b03f5f7f11d50a3a_4.0.10586.0_none_2a3e901af0c2891b\ThirdPartyNotices.txt
526
上記を実行すると
PowerShell 7.15 でも、正常(同様)に、実行されます。
PowerShell 7.4.6 でも、同様です。
|