Powershell Script to Create Registry Files to Change Powershell Execution Policy
In a prior post, I showed you how to manually configure your computer to enable ExecutionPolicy so you can run Powershell scripts. To make it even better, I now have a powershell script that will create the .REG files you need so all you have to do is run the script and double-click on the file. The script also shows some other pretty cool tricks like opening a dialog box to get a folder path and creating a text file. Simply Copy and paste the following code into PowerShell or PowerShell ISE and watch the magic
<#
================================================================================
========= Introduction to BuildRegFiles.ps1 =======================================
================================================================================
Name: BuildRegFiles.ps1
Purpose: Build Registry files needed to enable Execution of Powershell scripts
Author: Dan Stolts - dstolts@microsoft.com - https://ITProGuru.com
Syntax/Execution: Simply Copy entire script contents and paste into PowerShell (or ISE) :)
Then follow on-screen prompts
Description:
Creates Registry files and puts them in the default folder
Allows user to select default folder. If not selected, current folder will be used
Builds: <CurrentPath>\PowerShell-Execution-Unrestricted.reg
<CurrentPath>\PowerShell-Execution-RemoteSigned.reg
For Details on what these settings do... See
https://itproguru.com/expert/2012/01/how-to-create-enable-permissions-and-run-a-multi-line-powershell-script/
To Run the .REG files simply double-click on them in File Explorer and follow the prompts
================================================================================
#>
$MyPath = ((Get-Item -Path ".\").FullName + "\") #Set the default value for path
$object = New-Object -comObject Shell.Application
$folder = $object.BrowseForFolder(0, "Please select Location to save files", 0)
if ($folder -ne $null) {
$MyPath = $folder.self.Path.substring(0,$folder.self.path.length) # Set the Path
if ($folder.self.path.substring($folder.self.path.length - 1, 1) -ne "\") {
# Add Trailing backslash
$MyPath = $folder.self.Path.substring(0,$folder.self.path.length) + "\"}
}
Write-Host $MyPath "will be used for creating registry files" -ForegroundColor Green
Set-Location $MyPath
Write-Host (Get-Date) -ForegroundColor Green
$WritePath = $MyPath + "PowerShell-Execution-RemoteSigned.reg"
Write-Host "Creating" $WritePath -ForegroundColor Green
$SaveFile = 'Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell]
"ExecutionPolicy"="RemoteSigned"'
$fso = new-object -comobject scripting.filesystemobject
$file = $fso.CreateTextFile($WritePath,$true) #will overwrite any existing file
$file.write($SaveFile)
$file.close()
Write-Host (Get-Date) -ForegroundColor Green
$WritePath = $MyPath + "PowerShell-Execution-Unrestricted.reg"
Write-Host "Creating" $WritePath -ForegroundColor Green
$SaveFile = 'Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell]
"ExecutionPolicy"="Unrestricted"'
$fso = new-object -comobject scripting.filesystemobject
$file = $fso.CreateTextFile($WritePath,$true) #will overwrite any existing file
$file.write($SaveFile)
$file.close()
Write-Host "Finished" (Get-Date) -ForegroundColor Green