如何使用 PowerShell 从 GAC 中已存在的给定路径中删除所有 dll?

How to delete all dlls from given path that already exist in the GAC using PowerShell?

这里是否有任何 PowerShell 专家知道如何从 GAC 中已存在的给定路径中删除所有 dll?

您可以通过名称确定程序集是否已在 GAC 中:

$AssemblyName = [System.Reflection.AssemblyName]::GetAssemblyName("C:\Path\to\assembly.dll")
$IsInGAC = [System.Reflection.Assembly]::ReflectionOnlyLoad($AssemblyName).GlobalAssemblyCache

您可以将其包装在测试函数中以过滤您的输入程序集:

function Test-GACPresence {
    param(
        [Parameter(Mandatory=$true,ParameterSetName='Path')]
        [string]$Path,

        [Parameter(Mandatory=$true,ParameterSetName='LiteralPath',ValueFromPipelineByPropertyName=$true)]
        [Alias('PsPath')]
        [string]$LiteralPath
    ) 

    $LiteralPath = if($PSCmdlet.ParameterSetName -eq 'Path'){
        (Resolve-Path $Path).ProviderPath
    } else {
        (Resolve-Path $LiteralPath).ProviderPath
    }

    try{
        return [System.Reflection.Assembly]::ReflectionOnlyLoad([System.Reflection.AssemblyName]::GetAssemblyName($LiteralPath)).GlobalAssemblyCache
    }
    catch{
        return $false
    }
}

$ExistsInGAC = Get-ChildItem "path\to\test" -Filter *.dll -Recurse |?{$_|Test-GACPresence}
$ExistsInGAC |Remove-Item