A fairly good amount of time we spend on our system for managing, searching files, folders. And when you are in learning phase of any new scripting language (just like me, in PS), it is obvious to have an eagerness about “How to” work with files and folder in that Scripting lang. Fortunately, PowerShell makes scripting files and directories as easy as working at the command line. Being an administrative shell, PowerShell directly supports tasks such as visiting all the files in a subdirectory or moving a file from one directory to another.
To retrieve the list of files in a directory, use the Get-ChildItem cmdlet. To get a specific item, use the Get-Item cmdlet:
· To list all items in the current directory, use the Get-ChildItem cmdlet:
Get-ChildItem
· To list all items that match a wildcard, supply a wildcard to the Get-ChildItem cmdlet:
Get-ChildItem *.doc
· To list all files that match a wildcard in the current directory (and all its children), use the –Include and –Recurse parameters of the Get-ChildItem cmdlet:
Get-ChildItem –Include *.doc -Recurse
· To list all directories in the current directory, use the Where-Object cmdlet to test the PsIsContainer property:
Get-ChildItem | Where { $_.PsIsContainer }
· To get information about a specific item, use the Get-Item cmdlet:
Get-Item test.txt
More to come in next post…