If the error persists after changing execution policies, several factors might be interfering. Here's a systematic troubleshooting approach.
Check for conflicting policies:
# Detailed policy analysis
foreach ($scope in @('MachinePolicy','UserPolicy','Process','CurrentUser','LocalMachine')) {
$policy = Get-ExecutionPolicy -Scope $scope
Write-Host "$scope`: $policy"
}
Verify PowerShell version and edition:
$PSVersionTable
PowerShell 5.1 (Windows PowerShell) and PowerShell 7.x maintain separate execution policies. If you're using PowerShell 7.x, ensure you've set policies for the correct version:
# Check which PowerShell you're running
if ($PSVersionTable.PSVersion.Major -ge 7) {
Write-Host "Running PowerShell 7.x (pwsh.exe)"
} else {
Write-Host "Running Windows PowerShell 5.1 (powershell.exe)"
}
Clear PowerShell cache and restart:
# Clear PowerShell module cache
Remove-Item -Path "$env:LOCALAPPDATA\Microsoft\Windows\PowerShell\ModuleAnalysisCache" -Force -ErrorAction SilentlyContinue
Then restart PowerShell completely (close all windows and reopen).
Test with a minimal script:
# Create and test simple script
'Write-Host "Execution policy test successful"' | Out-File -FilePath test-policy.ps1 -Encoding UTF8
.\test-policy.ps1
Verification: The test script should run without errors. If it still fails, check Windows Event Logs under Applications and Services Logs > Microsoft > Windows > PowerShell > Operational for detailed error information.
Warning: If none of these steps work, your system may have additional security software or policies blocking PowerShell execution. Contact your IT administrator for enterprise environments.