Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to unblock all files in a directory.
Microsoft Scripting Guy, Ed Wilson, is here. Today is the day. As those of you who have been reading the Hey, Scripting Guy! Blog on a regular basis know, today is the speaker’s dinner (and scripting slumber party) for Windows PowerShell Saturday in Charlotte, North Carolina. The speaker’s dinner and slumber party take place at the house that script built, and the Scripting Wife and I are the hosts. Check the website to see if any tickets remain—they have been flying off the shelves this week. If at all possible, you do not want to miss Windows PowerShell Saturday in Charlotte because there is an all-star lineup of speakers, and with three tracks and lots of sessions to choose from, it will be a learning extravaganza. Having just returned from Redmond (or the “mother ship” as some of my more sarcastic friends refer to the Microsoft headquarters), I am definitely pumped up, psyched-out, hyped-up (or whatever enthusiastic adverb you wish to use to describe me). Teresa will have a hard time restraining me as I deliver my two presentations on Windows PowerShell cool stuff!
The problem with blocked files
So, when I was getting ready to head out to Redmond for the week, I needed to ensure I had my Hey, Scripting Guy! Blog template copied on my corporate laptop. I do not normally use this laptop to write my blog because it simplifies things if I use a machine connected to my test lab instead of having to demonstrate alternate credentials for every single command.
So I copied my template file to a USB drive from my lab environment computer, and pasted the template to my corporate network-joined laptop. Cool, sneaker net rules! Then I opened the file, and I was greeted with a non-working file. The Microsoft Word document had a big yellow bar across the top informing me that I am in Protected View. Well, that may be fine for reading a document, but when you are a Scripting Guy with lots of people with whom to meet, and lots of blogs to write, suffice to say, the Protected View does not work. Sure, I can click Enable Editing, but that is just annoying. This view is shown in the image that follows.
Unblocking all files in a folder
Luckily, Windows PowerShell 3.0 in Windows 8 has the Unblock-File cmdlet. In the past, I have used the Streams utility to perform this operation—in fact, I showed the Scripting Wife how to do this in Scripting Wife Learns About Unblocking Files in PowerShell. If you do not have Windows 8, you can still use the technique from that post to unblock files. It works great (even if the Scripting Wife did accuse me of breaking her computer).
To unblock all the files in a folder, I first use the Get-ChildItem cmdlet to return a list of FileInfo objects from the directory, and then I pipe them to the Unblock-File cmdlet. Because I am changing system state (modifying a potentially great number of files), I want to first ensure that the command does what I want it to do, so I use the WhatIf parameter. This is shown here. (gci is an alias for the Get-ChildItem cmdlet.)
That looks like exactly what I want to accomplish, so I remove the WhatIf parameter and run the following command.
gci c:\fso | Unblock-File
Like I said, nothing returns. So I need to perform a test to see if the command worked. I open my HSG-Template.docx file, and as you can see here, the yellow blocking bar is now removed.
That is about all there is to unblocking files with Windows PowerShell 3.0. If you want to control which files to unblock, use the parameters of the Get-ChildItem cmdlet to modify the way you return files.
Join me tomorrow when I will talk about more cool Windows PowerShell stuff.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
0 comments