{"id":34,"date":"2014-11-07T16:45:01","date_gmt":"2014-11-07T16:45:01","guid":{"rendered":"https:\/\/www.lieben.nu\/liebensraum\/?p=34"},"modified":"2014-11-07T16:45:01","modified_gmt":"2014-11-07T16:45:01","slug":"elevating-powershell-scripts-and-staying-in-the-script-folder","status":"publish","type":"post","link":"https:\/\/lieben.nu\/liebensraum\/2014\/11\/elevating-powershell-scripts-and-staying-in-the-script-folder\/","title":{"rendered":"Elevating powershell scripts, and staying in the script folder"},"content":{"rendered":"<p>Sometimes you want to be able to just double click your powershell scripts and see them work&#8230;.putting this code at the top of your script will do just that by detecting if the script is running as administrator with administrative priviledges. If not, the script will launch a new instance of itself with an elevation prompt.<\/p>\n<pre>$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition\nIf (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] \"Administrator\"))\n{\n $arguments = \"& '\" + $myinvocation.mycommand.definition + \"'\"\n Start-Process powershell -Verb runAs -ArgumentList $arguments\n Break\n }\n cd $scriptPath\n<rest of your code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you want to be able to just double click your powershell scripts and see them work&#8230;.putting this code at the top of your script will do just that by detecting if the script is running as administrator with administrative priviledges. If not, the script will launch a new instance of itself with an elevation &hellip; <a href=\"https:\/\/lieben.nu\/liebensraum\/2014\/11\/elevating-powershell-scripts-and-staying-in-the-script-folder\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Elevating powershell scripts, and staying in the script folder<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[4,39],"tags":[54],"class_list":["post-34","post","type-post","status-publish","format-standard","hentry","category-automation","category-powershell","tag-powershell"],"_links":{"self":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts\/34","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/comments?post=34"}],"version-history":[{"count":0,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts\/34\/revisions"}],"wp:attachment":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/media?parent=34"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/categories?post=34"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/tags?post=34"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}