Daniel Elliott
Daniel Elliott

Reputation: 22857

Unblock a file with PowerShell?

I am trying to have PowerShell unblock a file in Win2K8 R2.

Does anyone have a pointer as to the syntax?

Upvotes: 37

Views: 43123

Answers (13)

js2010
js2010

Reputation: 27423

You can search for blocked files like this:

get-item * -stream zone*

Then to unblock the files, pipe that to remove-item or "rm" to delete the zone.identifier streams:

get-item * -stream zone* | Remove-Item -whatif

In case you want recursive search:

get-childitem -recurse | get-item -stream zone*

Upvotes: 1

Adam
Adam

Reputation: 73

I'll have to amend @Mike 's answer: this won't work if there are spaces in $_.FullName (e.g. like in "C:\Program Files") so it has to be:

get-childitem -rec | % { cmd /c "echo.>""$($_.FullName)"":Zone.Identifier" }

Upvotes: 1

Bruno Bieri
Bruno Bieri

Reputation: 10236

To unblock a folder and it's subfolder recursive (>= PowerShell v3) you can use the Get-ChildItem (gci) command:

Get-ChildItem "C:\Temp\" -recurse | Unblock-File

where C:\Temp is the starting folder.

Upvotes: 4

Joey
Joey

Reputation: 354516

If you are using PowerShell v3, you can use the Unblock-File cmdlet.


The "blocking" part is simply an alternate data stream of the file, named "Zone.Identifier". You can display it in CMD by using input redirection (no other way to get to a stream in CMD, though):

H:\Downloads> more < test.exe:Zone.Identifier
[ZoneTransfer]
ZoneId=3

You can find them using dir /r on Windows Vista and later:

2009-10-24  12:18        54.538.056 test.exe
                                 24 test.exe:Zone.Identifier:$DATA

Also in CMD you can easily get rid of that by overwriting it (using output redirection, this time):

echo.>myDownloadedFile.exe:Zone.Identifier

which isn't quite the same as removing the ADS completely, but works in that Explorer doesn't complain anymore.

There doesn't seem to be native support for handling ADS from within PowerShell (as mentioned on The PowerShell Guy's blog here. That article also has some information how to get that functionality in PowerShell). You could, however, simply call cmd:

cmd /c "echo.>test.exe:Zone.Identifier"

That works from PowerShell as well.

Another option would be Mark Russinovich's streams utility which allows you to inspect a file's ADS and also to delete them. So

streams -d myDownloadedFile.exe

does work as well.

Upvotes: 50

Dinch
Dinch

Reputation: 612

If you are using PowerShell 3.0 or above vesion, Unblock-file PowerShell cmdlet should solve this problem with unblocking the file, even though if you don't have unblock button on the file properties window.

The Unblock-File cmdlet lets you open files that were downloaded from the Internet. It unblocks Windows PowerShell script files that were downloaded from the Internet so you can run them, even when the Windows PowerShell execution policy is RemoteSigned. By default, these files are blocked to protect the computer from untrusted files.

Just open the powerShell window and follow below syntax. To find more information about the syntax go to here

Example :

unblock-file -path C:\Downloads\MyFileName.chm

Unblock file with PowerShell screen shot

Warning: Do not unblock unsecure files.

Upvotes: 0

OzBob
OzBob

Reputation: 4520

If you server does not have Powershell > v3 ($PSVersionTable.PSVersion.Major -ge 3). Then use good old reliable DOS:

for /f "tokens=*" %f in ('dir /b *.*') do echo.>"%f":Zone.Identifier 

Upvotes: 1

Warren  P
Warren P

Reputation: 68902

I haven't seen any answer yet that seems to use the proper powershell cmdlets to do this.

Here we can find DLLs in the current folder that contain the zone.identifier:

Get-Item -Path .\*.dll -stream * | where {$_.Stream -eq "Zone.Identifier" }

Here we zap just only the unwanted streams, unlike some answers above that might damage other streams:

Remove-Item  -Path .\*.dll -stream Zone.Identifier

Upvotes: 0

Mike Chaliy
Mike Chaliy

Reputation: 26658

Oneliner to remove zone informarion(inspired by accepted answer) for all children (with correct quoting).

get-childitem -rec | % { cmd /c "echo.>""$($_.FullName)"":Zone.Identifier" }

Not strictly answer to the question, just want to make sure when I next come up with this problem there is solution already :).

PS. Works in PS 2.0

Upvotes: 5

Parrish
Parrish

Reputation: 177

new to posting in forums like this and this might be an old topic but here is what you are looking for.

get-item -Path "path to file(s)" -Stream "Zone.Identifier" -ErrorAction "SilentlyContinue"

This should list out files that are blocked only.

Unblock-File -Path "Path to blocked file(s)"

This will unblock them.

Upvotes: 4

Andy Arismendi
Andy Arismendi

Reputation: 52609

I wrote a little function that uses the Win32 API to delete the Zone.Identifier NTFS alternate data stream which is what Windows uses to determine whether a file is to be blocked.

.NET doesn't have access to alternate data streams so the function uses a technique called platform invoking to call the native Win32 API. The benefit of this over the some other solutions for PowerShell is that it supports the PowerShell pipeline so you can pipe a list of file paths or System.IO.FileInfo objects to the function. The function also doesn't have any external dependencies and actually deletes the alternate data stream instead of just deleting it's contents.

http://andyarismendi.blogspot.com/2012/02/unblocking-files-with-powershell.html

Upvotes: 1

Kent Boogaart
Kent Boogaart

Reputation: 178660

Do you mean this:

set-executionpolicy remotesigned

This will allow you to execute local scripts without them being signed, and remote ones if they are signed. More info available here.

Upvotes: -2

Geert
Geert

Reputation: 550

Remove the alternate file stream using Streams.exe see this post: http://www.paraesthesia.com/archive/2010/05/19/unblocking-multiple-files-at-once.aspx

Upvotes: 1

Jaykul
Jaykul

Reputation: 15824

The PoshCode module includes Set-DownloadFlag and Remove-DownloadFlag functions which work as advertised. :) I've just pulled that piece out into it's own script contribution http://poshcode.org/1430 ... it will work on PowerShell 1 too, if you use the New-Type function in place of Add-Type ( http://poshcode.org/720 )

Upvotes: 9

Related Questions