Unblocking Files with PowerShell v3

There are a number of new features in PowerShell v3 that while not huge like CIM or workflow are os significant help to the hard pressed administrator.  One of these is the Unblock-File cmdlet.

If you haven’t updated your help the online version is available at http://technet.microsoft.com/en-us/library/hh849924.aspx

To test it I downloaded the PowerShell v2 release notes from

http://www.microsoft.com/en-us/download/details.aspx?id=11539

 

The gives me a file

Windows Mangement Framework Release Notes en-US.rtf

 

When you download a file – internet explorer safety mechanisms kick in and the file is blocked. This prevents a number of things happening including running scripts

You can test if a file is blocked by right clicking the file and looking at its properties – it will have an Unblock button at the bottom right of the dialog.

You can also use PowerShell to do this.

First we need to identify the files that are blocked.  These are files with an Alternative Data Stream of “Zone.Identifier”

 

PS> Get-Item -Path “c:\test\Windows Mangement Framework Release Notes en-US.rtf” -Stream “Zone.Identifier”

   FileName: C:\test\Windows Mangement Framework Release Notes en-US.rtf

Stream                   Length
——                   ——
Zone.Identifier              26

 

If you try to test multiple files

Get-Item -Path c:\test\*.* -Stream “Zone.Identifier”

be prepared for lots of error messages when the system doesn’t find an alternative data stream.  Better still control the error messages

Get-Item -Path c:\test\*.* -Stream “Zone.Identifier” -ErrorAction SilentlyContinue

  FileName: C:\test\indice_analitico.pdf

Stream                   Length
——                   ——
Zone.Identifier              26

   FileName: C:\test\Windows Mangement Framework Release Notes en-US.rtf

Stream                   Length
——                   ——
Zone.Identifier              26

Now I don’t recognise that pdf file so I’ll leave that for later

Either of these will filter on the extension

Get-Item -Path c:\test\*.rtf -Stream “Zone.Identifier” -ErrorAction SilentlyContinue
Get-Item -Path c:\test\*.* -Filter *.rtf -Stream “Zone.Identifier” -ErrorAction SilentlyContinue

Once you have the file

Get-Item -Path c:\test\*.* -Filter *.rtf -Stream “Zone.Identifier” -ErrorAction SilentlyContinue | Unblock-File

won’t work because only the –LiteralPath parameter of Unblock-File takes pipeline input and that’s by property name

 

These two options will work

Get-Item -Path c:\test\*.* -Filter *.rtf -Stream “Zone.Identifier” -ErrorAction SilentlyContinue |
foreach {Unblock-File -Path $_.FileName }

 

Get-ChildItem -Path c:\test\*.* -Filter *.rtf | Unblock-File

 

I prefer the first because it allows me to test and then modify to perform the unblock.

This entry was posted in File system, PowerShell V3. Bookmark the permalink.

One Response to Unblocking Files with PowerShell v3

  1. James says:

    Excellent! I’ve been using streams.exe from the SysInternals suite to do this, but it’s nice to have it all in PowerShell now.

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