-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRun .ps1 files with PowerShell.ps1
More file actions
117 lines (90 loc) · 3.81 KB
/
Run .ps1 files with PowerShell.ps1
File metadata and controls
117 lines (90 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
############################## GLOBALS ##############################
$global:debug = 0
$global:display = 'Normal'
$global:title = 'Run .ps1 files with PowerShell'
$global:args = $args
############################## MAIN CODE ##############################
function main {
run_as_admin
show_title $global:title -set_title
$powershellPath = '"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"'
$powershellRun = "$powershellPath -File `"%1`" %*"
$powershellReg = 'Registry::HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1'
$powershellOpenWithReg = 'Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ps1'
Write-Host -NoNewLine "`n`n`t Setting Execution Policy... "
if (-Not $global:debug) { $ErrorActionPreference = 'SilentlyContinue' }
$NULL = Set-ExecutionPolicy 'RemoteSigned' -Scope 'CurrentUser' -Force # Default: "Restricted"
check_mark
Write-Host -NoNewLine "`n`n`t Fixing Run Command... "
Set-Item "$powershellReg\Shell\Open\Command" -Value $powershellRun # Default: "C:\Windows\System32\notepad.exe" "%1"
check_mark
Write-Host -NoNewLine "`n`n`t Fixing Icon... "
Set-Item "$powershellReg\DefaultIcon" -Value $powershellPath # Default: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell_ise.exe",1
check_mark
Write-Host -NoNewLine "`n`n`t Adding Drag & Drop... "
$NULL = Remove-Item "$powershellReg\ShellEx" -Recurse
$NULL = New-Item "$powershellReg\ShellEx\DropHandler" -Force
Set-Item "$powershellReg\ShellEx\DropHandler" -Value '{60254CA5-953B-11CF-8C96-00AA00B8708C}' # Default: {86C86720-42A0-1069-A2E8-08002B30309D}
check_mark
Write-Host -NoNewLine "`n`n`t Adding Run as Administrator... "
$NULL = Remove-Item "$powershellReg\Shell\RunAs" -Recurse
$NULL = New-Item "$powershellReg\Shell\RunAs\Command" -Force
$NULL = New-ItemProperty "$powershellReg\Shell\RunAs" 'HasLUAShield'
Copy-Item "$powershellReg\Shell\Open\Command" "$powershellReg\Shell\RunAs"
check_mark
Write-Host -NoNewLine "`n`n`t Removing Open With... "
$RegKey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey($powershellOpenWithReg, $TRUE)
$RegKey.DeleteSubKey('UserChoice', $TRUE)
$RegKey.Close()
check_mark
finish
}
############################## FUNCTIONS ##############################
function run_as_admin {
$has_admin_rights = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')
if (-not $has_admin_rights) {
Start-Process 'powershell' '-NoProfile -ExecutionPolicy Bypass', $(if ($NULL -ne $PSCommandPath) { "-File `"$PSCommandPath`" $global:args" } else { $MyInvocation.MyCommand.Definition -replace '"', "'" }) -WorkingDirectory $pwd -Verb 'RunAs' -WindowStyle $(if ($global:debug) { 'Normal' } else { $global:display })
if ($global:debug) { pause }
exit
}
}
function show_title {
param (
[Parameter(Mandatory)] [string] $title,
[switch] $set_title
)
if ($set_title) { $Host.UI.RawUI.WindowTitle = $title }
Write-Host "`n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ $title ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
}
function finish {
param (
[string] $title = 'Process Finished',
[switch] $restart
)
''
show_title $title
[system.media.systemsounds]::Beep.play()
if ($restart) {
Write-Host -ForegroundColor Yellow -NoNewline "`n`n`t`t Restarting is required, restart now? [y/n] "
do {
$user_choice = [console]::ReadKey($TRUE).Key
} until (@('Y', 'N') -contains $user_choice)
if ($user_choice -eq 'Y') {
Start-Process 'shutdown' '/t /t 0'
}
}
else {
Write-Host -ForegroundColor Yellow "`n`n`t`t`t (Press any key to exit)"
$NULL = [Console]::ReadKey($TRUE)
}
exit
}
function check_mark {
param (
[string] $symbol = 'v',
[string] $color = 'Green'
)
Write-Host -ForegroundColor $color $symbol
}
############################## RUN MAIN CODE ##############################
main