PowerShell “dir” Get-ChildItem – a very simple useful cmdlet…

Lately, I’ve been involved in working with historical data, creating and moving SQL databases backups in my developers environment.  So, in one of my new scripts I started using the .NET Framework “System.IO.DirectoryInfo($YourCurrentFolder)” to get access to the folders both methods and properties.

So, what kind of properties and methods I’m talking about?  Here’s a few:

Methods
1. *.getDirectories()
2. *.getFiles()
Properties
1. name
2. basename
3. fullname
4. extension

 

First, we need to start building our .NET collection of folder objects.  In this case, I’m are trying to find all of my SQL backups on my C: drive, which have the file extension of *.bak.  I’m creating a variable to save my results.

So, I starting working .NET System.IO.Directory namespace:

$Rootname = ‘c:\’; $dbak = New-Object System.IO.DirectoryInfo($Rootname);

$found_baks = $dbak.getfiles(“*.bak”,”AllDirectories”)            # -updated thx to Shay

This few .NET liners will give me a list of all directories with all my *.bak files. 

So, going back to using PowerShell cmdlets I’m using “ Dir “ to search for my *.bak files:  dir C:\ –recurse –filter *.bak  or the unix-like version ‘ ls c: -r -fi *.bak

It started to give me some results but it also started to display some errors during the search process.  The error is due to the Operating System protecting some of the system files areas.

This error will not stop the execution of the cmdlet.  But, if you want to stop the error message,  then use the parameter “ –ea SilentlyContinue “ to skip it.

dir C:\ –recurse –filter *.bak –ea SilentlyContinue

We run a clean search process, save the results into a variable, then identify and check all the *.bak files locations. Select only to view Name with DirectoryName.  Remember, to access you variable information, use the “gm” (get-member) cmdlet using the parameter “ -MemberType property ” to display the Property objects.

Now, we got the list and proceed to properly identify your SQL backups location and later decide what to do with these files.  You can decide to export the information in the variable to an output file: *.txt or *.csv for later use.

Simple and useful!!  Happy PowerShelling!!

Advertisements

About maxt2posh

I’m from Puerto Rico, have been working with computers since 1979. Throughout many years, I have worked with SQL Server Technologies, provided support to Windows Servers/Client machines, Microsoft Virtualization Technologies, and build some Visual Studio solutions. I’m a Microsoft PowerShell MVP since 2009 and MVP SAPIEN Technologies since 2010. I speak in most of the SQLSaturday, IT Pro and .NET camps events around the Florida’s State. Also founder of the Florida PowerShell User Group which meeting every 3rd Tuesday evening of the month.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s