Easily Unblock All Files in a Directory Using PowerShell

Easily Unblock All Files in a Directory Using PowerShell

  • Comments 11
  • Likes

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.

Image of document

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.)

Image of command output

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.

Image of document

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 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Always helpful. :)

  • Very nice, since it was / is quite hard to do it with PowerShell v1/v2 (or any other tool / scripting language). Keep adding more useful Cmdlets!

  • Very nice, since it was / is quite hard to do it with PowerShell v1/v2 (or any other tool / scripting language). Keep adding more useful Cmdlets!

  • @Livo von Buren, @Hican, @HicanNL You are welcome. I am glad you like the series. I intend to continue blogging about useful cmdlets and things I come across. This is one of the really cool things about PowerShell 3.0 is there are lots of cool things we can do without having to write a script.

  • Nice - thnx!  I made a function that will recurse through the subdirectories of a parent dir:

    Function Files {

       [CmdletBinding()]

       param (

    #    [parameter(Mandatory=$true,ValueFromPipeline=$true)]

    #    [string[]]$computername

       )

       BEGIN { }

       PROCESS {

       $path = C:\scripts

       $Folders = dir $path -recurse | Where {$_.PSIsContainer}

           foreach($folder in $Folders){

               Get-ChildItem $folder.FullName | Unblock-File -whatif

           }

       }

       END { }

    }

  • I forgot - uncomment the 2 params if you want to perform on a different computer or list of computers

  • these  are important files  unblock  them as  soon as possible

  • Get-ChildItem -recurse c:\fso | Unblock-File

    recurses through all files in subdirectories.

  • Is there any way to get the status of a file through powershell - to return just the blocked files?

  • thanks

  • Unblock all files:

    ls -r | unblock-file