Unblock-File

取消阻止从 Internet 下载的文件。

语法

Unblock-File
       [-Path] <String[]>
       [-WhatIf]
       [-Confirm]
       [<CommonParameters>]
Unblock-File
       -LiteralPath <String[]>
       [-WhatIf]
       [-Confirm]
       [<CommonParameters>]

说明

使用 Unblock-File cmdlet 可以打开从 Internet 下载的文件。 它取消阻止从 Internet 下载的 PowerShell 脚本文件,以便即使 PowerShell 执行策略 RemoteSigned也是如此。 默认情况下,会阻止这些文件来保护计算机免受不受信任的文件。

在使用 Unblock-File cmdlet 之前,请查看该文件及其源,并验证它是否安全打开。

在内部,Unblock-File cmdlet 将删除 Zone.Identifier 备用数据流,该数据流的值为“3”,以指示它已从 Internet 下载。

有关 PowerShell 执行策略的详细信息,请参阅 about_Execution_Policies

此 cmdlet 已在 Windows PowerShell 3.0 中引入。

示例

示例 1:取消阻止文件

PS C:\> Unblock-File -Path C:\Users\User01\Documents\Downloads\PowerShellTips.chm

此命令取消阻止 PowerShellTips.chm 文件。

示例 2:取消阻止多个文件

PS C:\> dir C:\Downloads\*PowerShell* | Unblock-File

此命令取消阻止其名称包括“PowerShell”的 C:\Downloads 目录中的所有文件。 在验证所有文件都安全之前,不要运行这样的命令。

示例 3:查找和取消阻止脚本

The first command uses the *Stream* parameter of the Get-Item cmdlet get files with the Zone.Identifier stream.Although you could pipe the output directly to the **Unblock-File** cmdlet (Get-Item * -Stream "Zone.Identifier" -ErrorAction SilentlyContinue | ForEach {Unblock-File $_.FileName}), it is prudent to review the file and confirm that it is safe before unblocking.
PS C:\> Get-Item * -Stream "Zone.Identifier" -ErrorAction SilentlyContinue
   FileName: C:\ps-test\Start-ActivityTracker.ps1

Stream                   Length
------                   ------
Zone.Identifier              26

The second command shows what happens when you run a blocked script in a PowerShell session in which the execution policy is **RemoteSigned**. The RemoteSigned policy prevents you from running scripts that are downloaded from the Internet unless they are digitally signed.
PS C:\> C:\ps-test\Start-ActivityTracker.ps1
c:\ps-test\Start-ActivityTracker.ps1 : File c:\ps-test\Start-ActivityTracker.ps1 cannot
be loaded. The file c:\ps-test\Start-ActivityTracker.ps1 is not digitally signed. The script
will not execute on the system. For more information, see about_Execution_Policies.

At line:1 char:1
+ c:\ps-test\Start-ActivityTracker.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

The third command uses the **Unblock-File** cmdlet to unblock the script so it can run in the session.
PS C:\> Get-Item C:\ps-test\Start-ActivityTracker.ps1 | Unblock-File

此命令演示如何查找和取消阻止 PowerShell 脚本。

参数

-Confirm

在运行 cmdlet 之前,提示你进行确认。

类型:SwitchParameter
别名:cf
Position:Named
默认值:False
必需:False
接受管道输入:False
接受通配符:False

-LiteralPath

指定要取消阻止的文件。 与 Path不同,LiteralPath 参数的值与键入时完全相同。 不会将任何字符解释为通配符。 如果路径包含转义字符,请将它括在单引号中。 单引号告知 PowerShell 不要将任何字符解释为转义序列。

类型:String[]
别名:PSPath
Position:Named
默认值:None
必需:True
接受管道输入:True
接受通配符:False

-Path

指定要取消阻止的文件。 支持通配符。

类型:String[]
Position:0
默认值:None
必需:True
接受管道输入:False
接受通配符:True

-WhatIf

显示 cmdlet 运行时会发生什么情况。 cmdlet 未运行。

类型:SwitchParameter
别名:wi
Position:Named
默认值:False
必需:False
接受管道输入:False
接受通配符:False

输入

String

可以通过管道将文件路径传递给 取消阻止文件

输出

None

此 cmdlet 不生成任何输出。

备注

  • Unblock-File cmdlet 仅适用于文件系统驱动器。
  • 取消阻止文件 执行与文件资源管理器中 属性 对话框中 取消阻止 按钮相同的操作。
  • 如果在未阻止的文件上使用 Unblock-File cmdlet,该命令对未阻止的文件没有影响,并且 cmdlet 不会生成错误。