windowspowershellexecutionpolicy

Powershell closing automatic when running .ps1 file


I'm trying to create this .ps1 file: see the full .ps1 markup

# Definieer het pad waar de bestanden worden gekopieerd
$sourcePath = "C:\Users\Mr.Fox\AppData\Local\Hogwarts Legacy\Saved"

# Definieer het pad waar de bestanden naartoe worden gekopieerd
$destinationPath = "D:\Games\Saved HL"

# Zoek naar de laatst gebruikte mapnummer
$latestFolder = Get-ChildItem $destinationPath | Where-Object { $_.PSIsContainer } | Sort-Object { [int]($_.Name -replace '^\D+') } -Descending | Select-Object -First 1
if ($latestFolder -eq $null) {
    $folderNumber = 1
} else {
    $folderNumber = [int]($latestFolder.Name -replace '^\D+') + 1
}

# Maak de nieuwe doelmap aan
$newFolderName = "Backup_$("{0:D2}" -f $folderNumber)"
$newFolderPath = Join-Path $destinationPath $newFolderName
if (-not (Test-Path $newFolderPath)) {
    New-Item -ItemType Directory -Path $newFolderPath | Out-Null
}

# Voeg uitvoer toe aan het logbestand
$logFile = Join-Path $destinationPath "backuplog.txt"
$date = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logEntry = "$date - Backup gemaakt naar map $newFolderPath"
Add-Content $logFile $logEntry

# Vraag de gebruiker om bevestiging voordat het script wordt uitgevoerd
$confirm = Read-Host "Weet je zeker dat je het script wilt uitvoeren? Typ 'ja' om door te gaan."
Write-Host "Bevestiging: $confirm"

if ($confirm -eq "ja") {
    # Kopieer de bestanden van de bron naar de nieuwe doelmap
    Copy-Item $sourcePath $newFolderPath -Recurse -Force

    Write-Host "Het backupproces is voltooid en de uitvoer is toegevoegd aan het logbestand."
} else {
    Write-Host "Script uitvoering geannuleerd."
}

# Zet de PowerShell uitvoeringsbeleid terug naar de oorspronkelijke instelling
if ((Get-ExecutionPolicy -Scope CurrentUser) -eq "RemoteSigned") {
    Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Restricted
}

Read-Host "Druk op een toets om door te gaan..."

Then I try to click run with PowerShell, but it closes on me and what Get-ExecutionPolicy -List is telling me is that the current user's policy is Restricted, but when I try to enable it, it just goes back to Restricted, and I don't know why.

Can you help me out?

PS C:\WINDOWS\system32> Get-ExecutionPolicy -List

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser      Restricted
 LocalMachine    RemoteSigned

I try to search online for a solution but didn't get very; I tried: https://stackoverflow.com/a/27755459

still the same problem

I get this response back when running Set-ExecutionPolicy Unrestricted

Execution Policy Change The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic at https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): y Set-ExecutionPolicy : Windows PowerShell updated your execution policy successfully, but the setting is overridden by a policy defined at a more specific scope. Due to the override, your shell will retain its current effective execution policy of Restricted. Type "Get-ExecutionPolicy -List" to view your execution policy settings. For more information ple ase see "Get-Help Set-ExecutionPolicy". At line:1 char:1

  • Set-ExecutionPolicy Unrestricted
  • + CategoryInfo          : PermissionDenied: (:) [Set-ExecutionPolicy], >SecurityException
    + FullyQualifiedErrorId : ExecutionPolicyOverride,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
    

in regedit standard has no value but 'ExecutionPolicy' is already to Unrestricted though but still i cant run the script


Solution

  • tl;dr

    Set-ExecutionPolicy -Scope CurrentUser Undefined -Force
    

    Read on for an explanation and alternatives.


    Background info:

    PowerShell's execution policies can be defined in multiple scopes, but only one scope's policy is the effective one in a given session.

    Your Get-ExecutionPolicy -List shows that Restricted is defined in the CurrentUser scope (specific to the current user), and RemoteSigned in the LocalMachine scope.

    Therefore, Restricted is the effective policy for you, preventing script execution.

    You have three options, depending on your needs:

    If you additionally want to change the LocalMachine-scope policy, you'll need to do so from an elevated session (run as adminstrator).