{"id":71842,"date":"2015-07-30T00:01:00","date_gmt":"2015-07-30T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/07\/30\/launch-elevated-powershell-shell\/"},"modified":"2019-02-18T09:46:47","modified_gmt":"2019-02-18T16:46:47","slug":"launch-elevated-powershell-shell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/launch-elevated-powershell-shell\/","title":{"rendered":"Launch Elevated PowerShell Shell"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Ed Wilson, Microsoft Scripting Guy, talks about launching an elevated Windows PowerShell shell.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today I had a great talk with Steve, the Scripting Manager. One of the things we talked about was the importance of community, and how the Windows PowerShell community seems to be really engaged with the Windows PowerShell team and with other documentation efforts.<\/p>\n<p>One of the great things that I love about the Windows PowerShell community is the interaction I have with them at user groups, conferences, and community events (such as the <a href=\"http:\/\/atltechstravaganza.com\/\">Atlanta TechStravaganza<\/a> that is coming up). One of the questions that I get on a fairly regular basis has to do with working with Windows PowerShell as a non-elevated user.<\/p>\n<p>Yesterday, I wrote a function that changes the title bar of my Windows PowerShell console based on whether the user is elevated (see <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/use-function-to-determine-elevation-of-powershell-console\/\">Use Function to Determine Elevation of PowerShell Console<\/a>).<\/p>\n<p>The corollary to that is, &quot;How do I elevate Windows PowerShell if I need to do so without having to create a second &ldquo;admin&rdquo; shortcut that automatically launches with Admin rights?&quot; The following image shows the Advanced Properties window of the shortcut. Making an &ldquo;admin&rdquo; shortcut is as simple as checking <b>Run as Administrator<\/b>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/Hsg-7-30-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/Hsg-7-30-15-01.png\" alt=\"Image of window\" title=\"Image of window\" \/><\/a><\/p>\n<p>In the *nix world, such a function is known as <i>sudo.<\/i> So, I decided to call my function <b>Start-ElevatedPowerShell <\/b>and then I create an alias named <b>sudo<\/b>:<\/p>\n<p style=\"margin-left:30px\">Function Start-ElevatedPowerShell<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;Start-Process PowerShell -Verb Runas<\/p>\n<p style=\"margin-left:30px\">&nbsp;}<\/p>\n<p style=\"margin-left:30px\">Set-Alias -Name sudo -Value Start-ElevatedPowerShell | out-null<\/p>\n<p>After I save my function and alias to my Windows PowerShell profile, I close my Windows PowerShell shell, and then open it again. I then type <b>sudo<\/b><i> <\/i>at the Windows PowerShell prompt, and the UAC prompt appears (strangely enough, it asks about allowing the program to make changes to my system. Hmmm, maybe it knows something I don&rsquo;t know).<\/p>\n<p>I say <b>OK<\/b>, and then my elevated Windows PowerShell prompt appears. This is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/Hsg-7-30-15-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/Hsg-7-30-15-02.png\" alt=\"Image of prompt\" title=\"Image of prompt\" \/><\/a><\/p>\n<p>Cool, it works. Now my Windows PowerShell profile looks like this:<\/p>\n<p style=\"margin-left:30px\">#&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p style=\"margin-left:30px\">#<\/p>\n<p style=\"margin-left:30px\"># PowerShell console profile<\/p>\n<p style=\"margin-left:30px\"># ed wilson, msft<\/p>\n<p style=\"margin-left:30px\">#<\/p>\n<p style=\"margin-left:30px\"># NOTES: contains five types of things: aliases, functions, psdrives,<\/p>\n<p style=\"margin-left:30px\"># variables and commands.<\/p>\n<p style=\"margin-left:30px\"># version 1.2<\/p>\n<p style=\"margin-left:30px\"># 7\/27\/2015<\/p>\n<p style=\"margin-left:30px\"># HSG 7-28-2015<\/p>\n<p style=\"margin-left:30px\">#&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p style=\"margin-left:30px\">#Aliases<\/p>\n<p style=\"margin-left:30px\">Set-Alias -Name ep -Value edit-profile | out-null<\/p>\n<p style=\"margin-left:30px\">Set-Alias -Name tch -Value Test-ConsoleHost | out-null<\/p>\n<p style=\"margin-left:30px\">Set-Alias -Name gfl -Value Get-ForwardLink | out-null<\/p>\n<p style=\"margin-left:30px\">Set-Alias -Name gwp -Value Get-WebPage | out-null<\/p>\n<p style=\"margin-left:30px\">Set-Alias -Name rifc -Value Replace-InvalidFileCharacters | out-null<\/p>\n<p style=\"margin-left:30px\">Set-Alias -Name gev -Value Get-EnumValues | out-null<\/p>\n<p style=\"margin-left:30px\">Set-Alias -Name sudo -Value Start-ElevatedPowerShell | out-null<\/p>\n<p style=\"margin-left:30px\">#Variables<\/p>\n<p style=\"margin-left:30px\">New-Variable -Name doc -Value &quot;$home\\documents&quot; `<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; -Description &quot;My documents library. Profile created&quot; `<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; -Option ReadOnly -Scope &quot;Global&quot;<\/p>\n<p style=\"margin-left:30px\">if(!(Test-Path variable:backupHome))<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;new-variable -name backupHome -value &quot;$doc\\WindowsPowerShell\\profileBackup&quot; `<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; -Description &quot;Folder for profile backups. Profile created&quot; `<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; -Option ReadOnly -Scope &quot;Global&quot;<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">#PS_Drives<\/p>\n<p style=\"margin-left:30px\">New-PSDrive -Name Mod -Root ($env:PSModulePath -split &#039;;&#039;)[0] `<\/p>\n<p style=\"margin-left:30px\">&nbsp;-PSProvider FileSystem | out-null<\/p>\n<p style=\"margin-left:30px\">#Functions<\/p>\n<p style=\"margin-left:30px\">Function Edit-Profile<\/p>\n<p style=\"margin-left:30px\">{ ISE $profile }<\/p>\n<p style=\"margin-left:30px\">Function Test-ConsoleHost<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;if(($host.Name -match &#039;consolehost&#039;)) {$true}<\/p>\n<p style=\"margin-left:30px\">&nbsp;Else {$false}&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">Function Replace-InvalidFileCharacters<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;Param ($stringIn,<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $replacementChar)<\/p>\n<p style=\"margin-left:30px\">&nbsp;# Replace-InvalidFileCharacters &quot;my?string&quot;<\/p>\n<p style=\"margin-left:30px\">&nbsp;# Replace-InvalidFileCharacters (get-date).tostring()<\/p>\n<p style=\"margin-left:30px\">&nbsp;$stringIN -replace &quot;[$( [System.IO.Path]::GetInvalidFileNameChars() )]&quot;, $replacementChar<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">Function Get-TranscriptName<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;$date = Get-Date -format s<\/p>\n<p style=\"margin-left:30px\">&nbsp; &quot;{0}.{1}.{2}.txt&quot; -f &quot;PowerShell_Transcript&quot;, $env:COMPUTERNAME,<\/p>\n<p style=\"margin-left:30px\">&nbsp; (rifc -stringIn $date.ToString() -replacementChar &quot;-&quot;) }<\/p>\n<p style=\"margin-left:30px\">Function Get-WebPage<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;Param($url)<\/p>\n<p style=\"margin-left:30px\">&nbsp;# Get-WebPage -url (Get-CmdletFwLink get-process)<\/p>\n<p style=\"margin-left:30px\">&nbsp;(New-Object -ComObject shell.application).open($url)<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">Function Get-ForwardLink<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;Param($cmdletName)<\/p>\n<p style=\"margin-left:30px\">&nbsp;# Get-WebPage -url (Get-CmdletFwLink get-process)<\/p>\n<p style=\"margin-left:30px\">&nbsp;(Get-Command $cmdletName).helpuri<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">Function BackUp-Profile<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;Param([string]$destination = $backupHome)<\/p>\n<p style=\"margin-left:30px\">&nbsp; if(!(test-path $destination))<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; {New-Item -Path $destination -ItemType directory -force | out-null}<\/p>\n<p style=\"margin-left:30px\">&nbsp; $date = Get-Date -Format s<\/p>\n<p style=\"margin-left:30px\">&nbsp; $backupName = &quot;{0}.{1}.{2}.{3}&quot; -f $env:COMPUTERNAME, $env:USERNAME,<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; (rifc -stringIn $date.ToString() -replacementChar &quot;-&quot;),<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; (Split-Path -Path $PROFILE -Leaf)<\/p>\n<p style=\"margin-left:30px\">&nbsp;copy-item -path $profile -destination &quot;$destination\\$backupName&quot; -force<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">Function get-enumValues<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;# get-enumValues -enum &quot;System.Diagnostics.Eventing.Reader.StandardEventLevel&quot;<\/p>\n<p style=\"margin-left:30px\">Param([string]$enum)<\/p>\n<p style=\"margin-left:30px\">$enumValues = @{}<\/p>\n<p style=\"margin-left:30px\">[enum]::getvalues([type]$enum) |<\/p>\n<p style=\"margin-left:30px\">ForEach-Object {<\/p>\n<p style=\"margin-left:30px\">$enumValues.add($_, $_.value__)<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">$enumValues<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">Function Test-IsAdmin<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&lt;#<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; .Synopsis<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Tests if the user is an administrator<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; .Description<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Returns true if a user is an administrator, false if the user is not an administrator&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; .Example<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Test-IsAdmin<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; #&gt;<\/p>\n<p style=\"margin-left:30px\">&nbsp;$identity = [Security.Principal.WindowsIdentity]::GetCurrent()<\/p>\n<p style=\"margin-left:30px\">&nbsp;$principal = New-Object Security.Principal.WindowsPrincipal $identity<\/p>\n<p style=\"margin-left:30px\">&nbsp;$principal.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">Function Start-ElevatedPowerShell<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;Start-Process PowerShell -Verb Runas<\/p>\n<p style=\"margin-left:30px\">&nbsp;}<\/p>\n<p style=\"margin-left:30px\">#Commands<\/p>\n<p style=\"margin-left:30px\">Set-Location c:\\<\/p>\n<p style=\"margin-left:30px\">If(tch) {Start-Transcript -Path (Join-Path -Path `<\/p>\n<p style=\"margin-left:30px\">&nbsp;$doc -ChildPath $(Get-TranscriptName))}<\/p>\n<p style=\"margin-left:30px\">BackUp-Profile<\/p>\n<p style=\"margin-left:30px\">if(Test-IsAdmin)<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; { $host.UI.RawUI.WindowTitle = &quot;Elevated PowerShell&quot; }<\/p>\n<p style=\"margin-left:30px\">else { $host.UI.RawUI.WindowTitle = &quot;Mr $($env:USERNAME) Non-elevated Posh&quot; }<\/p>\n<p>That is all there is to using Windows PowerShell to start an elevated shell. Profile Week will continue tomorrow when I will talk about more cool stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about launching an elevated Windows PowerShell shell. Microsoft Scripting Guy, Ed Wilson, is here. Today I had a great talk with Steve, the Scripting Manager. One of the things we talked about was the importance of community, and how the Windows PowerShell community seems to be really engaged [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,144,3,4,45],"class_list":["post-71842","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-profiles","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about launching an elevated Windows PowerShell shell. Microsoft Scripting Guy, Ed Wilson, is here. Today I had a great talk with Steve, the Scripting Manager. One of the things we talked about was the importance of community, and how the Windows PowerShell community seems to be really engaged [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71842","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=71842"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71842\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=71842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}