Powershell Get-ChildItem examples

I really like Powershell because you can do a lot of tasks very easily and it’s very integrated with Windows and .NET framework. So, it’s powerfull.

Get-ChildItem it’s a versatile and useful cmdlet that gets the items and child items in one or more specified locations. In this post I write some code examples that I hope you find it useful for everyday tasks.

All the examples are against E:\Temp folder, just replace it for the one of your convinience.

# Get files and directories in E:\temp directoryGet-ChildItem-path"E:\temp"# To know the result object, the functions and properties we have, we can use Get-MemberGet-ChildItem-path"E:\temp"|Get-Member

We get two types returned, System.IO.DirectoryInfo and System.IO.FileInfo, depending if the objects are directories or files. Both classes inherits from System.IO.FileSystemInfo, so they have a lot of functions and properties in common. We can easily check this by filtering for folders or files with the NoteProperty PsIsContainer that returns true for folder and false for files:

So, after this short introduction now we’ll see the Get-ChildItem examples. Just to point that I like to use ? and % powershell abreviations:
- ? is an abreviation of Where-Object
- % is an abreviation of Foreach

# Count all directories with files and directories in each of themGet-ChildItem-path"E:\temp\prova\provaCopia"-Recurse|Where-Object{$_.PsIsContainer}|Foreach{$_.FullName+" -- "+$_.GetFiles().Count+" -- "+$_.GetDirectories().Count}# or abreviatedGet-ChildItem-path"E:\temp\prova\provaCopia"-Recurse|?{$_.PsIsContainer}|%{$_.FullName+" -- "+$_.GetFiles().Count+" -- "+$_.GetDirectories().Count}# Get empty directoriesGet-ChildItem-path"E:\temp"-Recurse|?{$_.PsIsContainer}|?{$_.GetFiles().Count-eq0}# or simplified:Get-ChildItem-path"E:\temp\prova"-Recurse|?{$_.PsIsContainer-and$_.GetFiles().Count-eq0}# Files that starts with "postgresql" (we use to lower to avoid case sensitive behaviour)Get-ChildItem-path"E:\temp"|?{$_.name.ToLower().StartsWith("postgresql")}

It’s very interesting to work with the date information that’s available with the properties LastAccessTime, CreationTime and LastWriteTime. The names are explain the meaning for themself, but be awere of some behaviours:
- When a file is copied, the CreationTime is changed, but LastWriteTime isn’t modified (it’s the same as the original file).
- When a file is edited, the LastWriteTime is modified.
- LastAccessTime: I have seen different behaviours depending the Windows version. Test it before using it.

# Files modified after being createdGet-ChildItem-path"E:\temp"|?{$_.CreationTime-ne$_.LastWriteTime}# Files that haven't been used for 2 months, recursiveGet-ChildItem-path"E:\temp"-Recurse|?{$_.LastAccessTime-lt(Get-Date).AddMonths(-2)}|%{Write-Output$_.FullName}

And to finish, a useful script to deletes old files or backups no longer needed. The condition can be used LastAccessTime, CreationTime or LastWriteTime, depending on your needs. Because it’s an example, I use the -WhatIf parameter in the Remove-Item cmdlet just to show what would be deleted.

# Files to be removed in temp folder, no subfoldersGet-ChildItem-path"E:\temp\"|?{$_.CreationTime-lt(Get-Date).AddMonths(-2)}|Remove-Item-Force-WhatIf# Using the -Recurse subfolders and it's files will be deletedGet-ChildItem-path"E:\temp\"-Recurse|?{$_.CreationTime-lt(Get-Date).AddMonths(-2)}|Remove-Item-Force-Recurse-WhatIf

NOTE: Be careful that when a folder satisfies the condition, it will delete all the folders and files recursively in it even if they don’t satisfy the condition. -WhatIf option says that the folder would remove but not the files that are in it that don’t satisfy it, but they also will be deleted!!