Home » PowerShell » Use PowerShell To Unblock Files

Use PowerShell To Unblock Files

Unblock-File cmdlet in PowerShell is used to unblock files that were downloaded from the internet. Using the command, you can unblock all files in a directory recursively, and unblock Powershell script (ps1) file.

PowerShell Unblock a File
PowerShell Unblock a File

The following PowerShell script uses the Unblock-File cmdlet to remove the Zone.Identifier stream from a file when downloaded from the internet.

Unblock-File -Path D:\PS\ps-convert.xlsx  

The output of the above PowerShell script unblocks a file specified in the given directory and marks it as safe for execution, thereby bypassing any security restrictions.

In this tutorial, we will discuss how to unblock a file, unblock multiple files in a folder, and unblock exe and ps1 files that were downloaded from the internet or untrusted sources.

Unblocking an Exe File

To unblock an exe file, you can use the Unblock-File cmdlet followed by the file’s full path.

Unblock-File -Path D:\PS\agar.exe

Unblock All Files in a Directory

You can unblock all files in a specified directory using the Get-ChildItem cmdlet to retrieve the files and then pipe them into the Unblock-File cmdlet.

Get-ChildItem -Path "D:\PS\ps-study\" | Unblock-File 

In the above PowerShell script, the Get-ChildItem command uses the Path parameter to retrieve all files from the specified directory and pipe them into the Unblock-File cmdlet to unblock all files in a folder.

Unblock Files Recursively

To unblock files in the specified target directory and in its subdirectories, you can use the -Recurse parameter with Get-ChildItem.

Get-ChildItem -Path "D:\PS\ps-study\" -Recurse | Unblock-File 

In the above PowerShell script, the Get-ChildItem command uses the parameter -Recurse to recursively retrieve all files from the specified target directory and its subfolders and pipe them into Unblock-File. The Unblock-File command unblocks all files and lets you open and use the files.

Unblocking Multiple Files

You can unblock multiple files simultaneously by providing the list of file paths.

# List of file paths
$Files = "D:\PS\ps-study\ps-convert.xlsx", "D:\PS\ps-study\ps-script.ps1","D:\argr.exe" 

foreach ($File in $Files) {
    Unblock-File -Path $File
}

In the above PowerShell script, the $Files variable stores the list of file paths. Each file path in the $Files array is processed one by one using the foreach loop. The Unblock-File cmdlet is used for each file path to unblock the files.

Unblocking Files in Network Shres

You can use the Unblock-File cmdlet in PowerShell to unblock files located in the network shared using their UNC paths.

Unblock-File -Path "\\incorp-eu-it\share\software\setupScreenSaver.ps1"

In the above PowerShell script, the Unblock-File cmdlet unblocks a .ps1 file located in the network share path.

Conclusion

I hope the above article on how to unblock a file and unblock all files in the directory and subdirectory using the PowerShell Unblock-File cmdlet is helpful to you.

Before using the Unblock-File cmdlet, review the file and its source. Make sure to verify that it is safe to open before you open the file.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.