[PS]Drive to Successful with Powershell

Grumpy Admin is lazy – In Windows there are often times when you want to copy files from one folder to another. My boss, tends to open up the source directory and then the destination directory and does the good old fashion drag and drop method. This works but can be quite inefficient really when you think about it. Count the number of clicks.  IT Administrators are lazy, that what I say – If there is a short way to do things then that is the way to do it! Inefficiency makes me grumpy! Wouldn’t it be great if you had an option where you could just right click it and then choose via a dialog box where you want to copy your selected files too!

There is the “send to option”, which is good for roots drives of USB sticks and the “My Documents” Folder, but it is all a bit limited, and lack of choice makes me grumpy as well. I think I need some professional anger management, Charlie Sheen style perhaps, with plenty of sex!

Well, I have previously mentioned in other posts about GUID’s being shortcuts to Explorer options and features, which with a small Registry addition can activated or using GUID as file extensions.

Using a GUID we can add a menu item to the right click and create a Copy To Folder option. This is a great tweak and addition and I don’t know why it isn’t there by default.  If you are experienced old, like me you will remember things the old Windows Powertoys for Windows 95. Where there was a “Tweak UI” application which did quite a few tweaks like this and “Send to” Extensions.

 

It is kind of handy and neat and can save time. It been installed on my work laptop client for a while now, and when someone is watching over you copy files, they see this option and are like, “that neat where did you get that”…

Basically you need to create a new registry key under the following hive – HKEY_CLASSES_ROOT. The full path to the area that you want to modify is

HKEY_CLASSES_ROOT\AllFilesystemObjects\shellex\ContextMenuHandlers

Here you need to create a new “key” and in the default value – just put the following GUID {C2FBB630-2971-11D1-A18C-00C04FD75D13} – Restart explorer and you should have the context menu.

So that is great, easy and a top tip! Creating Reg keys like this is easy especially in the GUI tool Regedit, we have all done it before… we could even use group policy to deploy this key out etc.  But this tip gives me the chance to demonstrate to you the PSdrive provider methods in PowerShell. So shall we have a look at how we can make this registry change in PowerShell. Might be useful to some of you out there!

PowerShell has a great feature in the fact that it has a method of encapsulation things like file systems, certificate stores, and registry hives as objects, and as objects can then be manipulated.  It does these through the PSdrive cmdlets. Let’s do a

Get-Command *psdrive

This is great there is a get-psdrive – let’s run this now

Get-PSDrive

From the screen capture, you can see that there is some Registry providers already defined, there is HKCU and HKLM – which are mapped to Current User and Local Machine – but hand on – we want to modify a reg setting in the Classes Root… I don’t see that listed.   Well your right, it isn’t listed. But that doesn’t mean we can’t create a new one 🙂 As you will see there was a new-psdrive cmdlet.  So let have a look shall we

Get-Help New-PSdrive

Handy – so we can now create a new PS-Drive which will use the Registry as the provider – so the command will be like this :-

New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT

And as ever, we confirm it with another Get-PSdrive :-

Get-PSDrive

So we now have our HKCR key defined as PSDrive, which points to the Registry and the HKEY_CLASSES_ROOT – Now what do we do with it!  Well the first thing will be to enumerate it and make sure it is what we expect it is. PS Drive makes it traversable like a drive letter.  So we can do the old fashion cmd like this to get to this new drive.

cd HKCR:

And then we can do a Get-ChildItem or GCI or if you feel old you can do a “DIR” on it 🙂 and this list that Reg Hive as if it was a file system.

Excellent so what now… let do another CD to our first level that we want – it is the registry after all so let’s take it slow to start with.

cd AllFileSystemObjects

GCI

As you can see from the screen print we are now navigating the registry with easy – so let’s get to the right depth and to the area of the hive that we want to be in. Which is \shellex\ContextMenuHandlers\

cd \shellex\ContextMenuHandlers\

GCI

so as it’s a being treated like a file system, let’s throw a new-item at it, as Grump Admin is lazy and we already navigated to the path, to “check” we can be lazy and use the “.\” as the path in the new-item cmdlet

New-Item -path “.\” -name “Copy To”

As you can see there are no properties to this new key – so umm let give it a property of that GUID

New-ItemProperty -path “.\Copy To” -Name “(default)” -Value “{C2FBB630-2971-11D1-A18C-00C04FD75D13}”

Once again let’s confirm and do a DIR this time just to prove that works and we will see our key listed and let’s also check RegEdit just to be sure the key has been created.

Job done… all that is left to do is to restart explorer and get using our new context menu…

So as you can see it is very easy to manipulate Registry hives, using the PDDrive. When it is accessed like a file system – Don’t forget you can use the Get-ItemProperty cmdlet and the pipeline for conditional where-object statements. There so much you can do with this, but don’t forget don’t blame Grumpy Admin if you mess it all up!
Have fun

Hazzy