Scripting Files with PowerShell's - Get-Childitem (gci)Scripting Files with PowerShell's - Get-Childitem (gci)Sooner or later you need a script which lists the files in a folder. In DOS we would type: 'DIR'; the nearest equivalent in PowerShell is gci. The full name behind the alias of gci is the informative, get-ChildItem. You can take the comparison further, dir /s in DOS, translates to get-ChildItem -recurse in PowerShell. get-ChildItem Topics
Trusty Twosome (get-Help and get-Member)When you discover a new PowerShell command, it benefits from being probed with what I call the 'Trusty Twosome'. Experiment with get-Help and get-Member and unlock new scripting possibilities for get-Childitem. To see what I mean try these two commands: 1) get-help get-Childitem get-help unearths useful parameters such as -recurse, -exclude, it also reveals hidden files with: -force. 2) get-Childitem | get-Member get-Member reveals properties that you don't normally see in explorer, for example, CreationTime. Example 1 - List files in the root of the C:\ driveHere is an example which lists all the files in the C:\ root. If you need any help in executing the code, then see here. # Powershell script to list the files in C: root Note 1: In this instance C:\ is the root and get-Childitem lists any files in this directory. Note 2: get-Childitem "C:\" works equally well without the speech
marks. For example: Example 2 - List ALL files, including hidden and system$i=0 Note 1: The key addition is the parameter -force. What this does is include hidden and system files. Note 2: The additional commands enable us to count the files, this makes easier to see prove that -force really makes a difference. Double check what I mean by running the script with, and without, the -force switch. Example 3 - Filter to list just the System files# PowerShell cmdlet to list the System files in the root of C:\ Note 1: We need to employ the comparison parameter -match "System", rather than -eq "System", this is because System files also have Hidden and other attributes. Consequently, their attribute does not equal ''System'', although it does contain, or match the word System. Note 2: You probably worked it out for your self, but just to emphasise that the variable $i is a counter. Moreover, ++ increments $i each time the 'where' statements makes a match. Incidentally, omitting $=0 at the beginning produces an unexpected, or undesirable result when you run the script for a second time. Challenge 1: Repeat the command with and without -force. Challenge 2: Substitute this new 'where' clause on line 3: where {$_.attributes -ne "Directory"}. -ne is the opposite of -eq. Thus this command filters out the directory entry. # PowerShell cmdlet to list the System files in the root of C:\
˚
Example 4 - The famous -recurse parameterOutside of PowerShell, recurse is a little known verb meaning rerun. Inside of PowerShell, -recurse is one of the most famous parameters meaning: repeat the procedure on sub-folders. The point of this script is to list all the executables in the System32 folder. Moreover it will display the CreationTime, which can help determine if a file is up-to-date. This technique is particularly useful for troubleshooting .dll files. # Powershell script to list the exe files under C:\Windows\System32 Note 1: This example uses two aliases; my principle reason was to make line 4 shorter. Gci is an alias for our featured command get-Childitem, and the question mark (?) is an alias for 'where'. Note 2: The -recurse parameter comes directly after the name of the directory. For completeness, the location should be introduced by the -path parameter, but as this is
optional, I have omitted the -path parameter in this script. However, this is how to include the -path parameter. Note 3: If you investigate the file's properties, then you can spot other assets, for example: LastWriteTime, or even Length. Here is how to employ get-Member to list the properties. # Powershell script to investigate file properties get-Childitem -recurse can be surprisingly tricky, if this construction is giving you problems, see here for more help on -recurse The Rest of the Item FamilyCopy-Item See more PowerShell real life tasks• PowerShell Home • Real life tasks • Eventlog • Exchange • Com objects • Services • Syntax Please write in if you see errors of any kind. Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.
*
|
||||||