So last time we saw with Powershell it was very easy to pull down files based upon Date and Time. That he had far more power to query the filesystem as if it were a database. Which in a sense it is.
But what is one of the more common operations we do on our file system ? We clean up old data. We delete garbage that is just that (trash) cluttering up the file system (like temporary files). Sometimes we move older data somewhere else.
But the problem we’ve always had to deal with was if we wrote a query in DOS or vbScript, well it was a bit scary. You would have to create a test structure REALLY if you were doing anything complex to avoid “Murphy”
But Powershell changed the scope with one simple parameter
“-WHATIF” does exactly what you think it would do. “What IF” I did this? What would “IT” do? This parameter gives you the ability to test a “Destructive Cmdlet” on live data without actually hurting it. When run it gives you a line by line view of what it was GOING to do IF you allowed it to run through
This parameter is used in Multiple Cmdlets that do things that are usually pretty difficult to roll back (like deleting objects in Active Directory, Removing Files / folders or chopping off Branches in the Registry Tree)
So let’s take a look at an example. If we took a directory of objects in the file system with GET-CHILDITEM
Which shows us a list of files in the C:\Powershell\ Folder. Now all we need to do to remove anything in that list of Objects is pipe it through REMOVE-ITEM. But really, safety first so we’re going to add on the “-WHATIF” parameter
GET-CHILDITEM C:\Powershell\* | REMOVE-ITEM –WHATIF
Running this command will give us this type of an output
But running a GET-CHILDITEM on that same folder will confirm all is there still. This is one of the simplest and best features in Powershell. “-WHATIF” let’s you do something, (and if you’re not sure) not hurt ANYTHING in the process.
So if your INTENTION was to only remove those old text files and NOT your entire folder, you were still safe. You could just as easily query Files by type, date / time, recurse into folder trees and STILL use “-WHATIF”
Next time we’ll see just how easy it is to navigate the registry in Powershell.
The Power of Shell is in YOU
The Energized Tech