{"id":1628,"date":"2009-09-02T14:57:44","date_gmt":"2009-09-02T22:57:44","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=1628"},"modified":"2009-09-02T14:58:30","modified_gmt":"2009-09-02T22:58:30","slug":"powershell-hosting-and-myinvocation","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2009\/09\/02\/powershell-hosting-and-myinvocation\/","title":{"rendered":"PowerShell hosting and $MyInvocation"},"content":{"rendered":"<p>Many PowerShell users utilize code as shown below to determine where the running script resides:<\/p>\n<p>function get-scriptdirectory{<br \/>&nbsp; $invocation=(get-variable MyInvocation -Scope 1).Value<br \/>&nbsp; Split-Path $invocation.myinvocation.path<br \/>}<\/p>\n<p>Unfortunately, if the script runs inside a custom host, such as PrimalScript&#8217;s internal host, for a good number of debuggers and the SAPIEN PowerShell host from the PrimalForms packager, the MyInvocation variable contains an empty path member.<\/p>\n<p>There is no documented way to set MyInvocation from a custom host and any attempts contacting Microsoft in this matter only resulted in confirmation that any attempt to do so would be overwritten anyway.<\/p>\n<p>It seems unreasonable to expect the PowerShell team to change the API anytime soon. And even then, changing the current behavior might prove undesirable since it could potentially break existing scripts.<\/p>\n<p>That of course leaves everyone implementing a custom host with a dilemma. The SAPIEN PowerShell hosts now all contain a variable called $HostInvocation which has the same interface as MyInvocation and provides the desired information.<\/p>\n<p>So if you are running your script inside any SAPIEN PowerShell host you can use code like this to determine where your script is:<\/p>\n<p>function get-scriptdirectory{<br \/>&nbsp; if($hostinvocation -ne null) {<br \/>&nbsp;&nbsp;&nbsp; Split-Path $hostinvocation.MyCommand.path<br \/>&nbsp; }<br \/>&nbsp; else {<br \/>&nbsp;&nbsp;&nbsp; $invocation=(get-variable MyInvocation -Scope 1).Value<br \/>&nbsp;&nbsp;&nbsp; Split-Path $invocation.myinvocation.path<br \/>&nbsp; }<br \/>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many PowerShell users utilize code as shown below to determine where the running script resides: function get-scriptdirectory{&nbsp; $invocation=(get-variable MyInvocation -Scope 1).Value&nbsp; Split-Path $invocation.myinvocation.path} Unfortunately, if the script runs inside a custom host, such as PrimalScript&#8217;s internal host, for a good number of debuggers and the SAPIEN PowerShell host from the PrimalForms packager, the MyInvocation variable [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[283,25],"tags":[521,522,28],"class_list":["post-1628","post","type-post","status-publish","format-standard","hentry","category-howto","category-windows-powershell","tag-hosting","tag-myinvocation","tag-powershell"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"Many PowerShell users utilize code as shown below to determine where the running script resides: function get-scriptdirectory{ $invocation=(get-variable MyInvocation -Scope 1).Value Split-Path $invocation.myinvocation.path} Unfortunately, if the script runs inside a custom host, such as PrimalScript&#039;s internal host, for a good number of debuggers and the SAPIEN PowerShell host from the PrimalForms packager, the MyInvocation variable\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Alex Riedel\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/dev.sapien.com\/blog\/2009\/09\/02\/powershell-hosting-and-myinvocation\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"DEV SAPIEN Blog - Tools for IT Success\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"PowerShell hosting and $MyInvocation - DEV SAPIEN Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"Many PowerShell users utilize code as shown below to determine where the running script resides: function get-scriptdirectory{ $invocation=(get-variable MyInvocation -Scope 1).Value Split-Path $invocation.myinvocation.path} Unfortunately, if the script runs inside a custom host, such as PrimalScript&#039;s internal host, for a good number of debuggers and the SAPIEN PowerShell host from the PrimalForms packager, the MyInvocation variable\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/dev.sapien.com\/blog\/2009\/09\/02\/powershell-hosting-and-myinvocation\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2009-09-02T22:57:44+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2009-09-02T22:58:30+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:title\" content=\"PowerShell hosting and $MyInvocation - DEV SAPIEN Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Many PowerShell users utilize code as shown below to determine where the running script resides: function get-scriptdirectory{ $invocation=(get-variable MyInvocation -Scope 1).Value Split-Path $invocation.myinvocation.path} Unfortunately, if the script runs inside a custom host, such as PrimalScript&#039;s internal host, for a good number of debuggers and the SAPIEN PowerShell host from the PrimalForms packager, the MyInvocation variable\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/2009\\\/09\\\/02\\\/powershell-hosting-and-myinvocation\\\/#blogposting\",\"name\":\"PowerShell hosting and $MyInvocation - DEV SAPIEN Blog\",\"headline\":\"PowerShell hosting and $MyInvocation\",\"author\":{\"@id\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/author\\\/alexander-riedel\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/#organization\"},\"datePublished\":\"2009-09-02T14:57:44-07:00\",\"dateModified\":\"2009-09-02T14:58:30-07:00\",\"inLanguage\":\"en-US\",\"commentCount\":2,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/2009\\\/09\\\/02\\\/powershell-hosting-and-myinvocation\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/2009\\\/09\\\/02\\\/powershell-hosting-and-myinvocation\\\/#webpage\"},\"articleSection\":\"Howto, Windows PowerShell, Hosting, MyInvocation, powershell\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/2009\\\/09\\\/02\\\/powershell-hosting-and-myinvocation\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/dev.sapien.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/dev.sapien.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/topics\\\/windows-powershell\\\/#listItem\",\"name\":\"Windows PowerShell\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/topics\\\/windows-powershell\\\/#listItem\",\"position\":2,\"name\":\"Windows PowerShell\",\"item\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/topics\\\/windows-powershell\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/2009\\\/09\\\/02\\\/powershell-hosting-and-myinvocation\\\/#listItem\",\"name\":\"PowerShell hosting and $MyInvocation\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/dev.sapien.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/2009\\\/09\\\/02\\\/powershell-hosting-and-myinvocation\\\/#listItem\",\"position\":3,\"name\":\"PowerShell hosting and $MyInvocation\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/topics\\\/windows-powershell\\\/#listItem\",\"name\":\"Windows PowerShell\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/#organization\",\"name\":\"DEV SAPIEN Blog\",\"description\":\"Tools for IT Success\",\"url\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/author\\\/alexander-riedel\\\/#author\",\"url\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/author\\\/alexander-riedel\\\/\",\"name\":\"Alex Riedel\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/2009\\\/09\\\/02\\\/powershell-hosting-and-myinvocation\\\/#webpage\",\"url\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/2009\\\/09\\\/02\\\/powershell-hosting-and-myinvocation\\\/\",\"name\":\"PowerShell hosting and $MyInvocation - DEV SAPIEN Blog\",\"description\":\"Many PowerShell users utilize code as shown below to determine where the running script resides: function get-scriptdirectory{ $invocation=(get-variable MyInvocation -Scope 1).Value Split-Path $invocation.myinvocation.path} Unfortunately, if the script runs inside a custom host, such as PrimalScript's internal host, for a good number of debuggers and the SAPIEN PowerShell host from the PrimalForms packager, the MyInvocation variable\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/2009\\\/09\\\/02\\\/powershell-hosting-and-myinvocation\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/author\\\/alexander-riedel\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/author\\\/alexander-riedel\\\/#author\"},\"datePublished\":\"2009-09-02T14:57:44-07:00\",\"dateModified\":\"2009-09-02T14:58:30-07:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/\",\"name\":\"DEV SAPIEN Blog\",\"description\":\"Tools for IT Success\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/dev.sapien.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"PowerShell hosting and $MyInvocation - DEV SAPIEN Blog","description":"Many PowerShell users utilize code as shown below to determine where the running script resides: function get-scriptdirectory{ $invocation=(get-variable MyInvocation -Scope 1).Value Split-Path $invocation.myinvocation.path} Unfortunately, if the script runs inside a custom host, such as PrimalScript's internal host, for a good number of debuggers and the SAPIEN PowerShell host from the PrimalForms packager, the MyInvocation variable","canonical_url":"https:\/\/dev.sapien.com\/blog\/2009\/09\/02\/powershell-hosting-and-myinvocation\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/dev.sapien.com\/blog\/2009\/09\/02\/powershell-hosting-and-myinvocation\/#blogposting","name":"PowerShell hosting and $MyInvocation - DEV SAPIEN Blog","headline":"PowerShell hosting and $MyInvocation","author":{"@id":"https:\/\/dev.sapien.com\/blog\/author\/alexander-riedel\/#author"},"publisher":{"@id":"https:\/\/dev.sapien.com\/blog\/#organization"},"datePublished":"2009-09-02T14:57:44-07:00","dateModified":"2009-09-02T14:58:30-07:00","inLanguage":"en-US","commentCount":2,"mainEntityOfPage":{"@id":"https:\/\/dev.sapien.com\/blog\/2009\/09\/02\/powershell-hosting-and-myinvocation\/#webpage"},"isPartOf":{"@id":"https:\/\/dev.sapien.com\/blog\/2009\/09\/02\/powershell-hosting-and-myinvocation\/#webpage"},"articleSection":"Howto, Windows PowerShell, Hosting, MyInvocation, powershell"},{"@type":"BreadcrumbList","@id":"https:\/\/dev.sapien.com\/blog\/2009\/09\/02\/powershell-hosting-and-myinvocation\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/dev.sapien.com\/blog#listItem","position":1,"name":"Home","item":"https:\/\/dev.sapien.com\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/dev.sapien.com\/blog\/topics\/windows-powershell\/#listItem","name":"Windows PowerShell"}},{"@type":"ListItem","@id":"https:\/\/dev.sapien.com\/blog\/topics\/windows-powershell\/#listItem","position":2,"name":"Windows PowerShell","item":"https:\/\/dev.sapien.com\/blog\/topics\/windows-powershell\/","nextItem":{"@type":"ListItem","@id":"https:\/\/dev.sapien.com\/blog\/2009\/09\/02\/powershell-hosting-and-myinvocation\/#listItem","name":"PowerShell hosting and $MyInvocation"},"previousItem":{"@type":"ListItem","@id":"https:\/\/dev.sapien.com\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/dev.sapien.com\/blog\/2009\/09\/02\/powershell-hosting-and-myinvocation\/#listItem","position":3,"name":"PowerShell hosting and $MyInvocation","previousItem":{"@type":"ListItem","@id":"https:\/\/dev.sapien.com\/blog\/topics\/windows-powershell\/#listItem","name":"Windows PowerShell"}}]},{"@type":"Organization","@id":"https:\/\/dev.sapien.com\/blog\/#organization","name":"DEV SAPIEN Blog","description":"Tools for IT Success","url":"https:\/\/dev.sapien.com\/blog\/"},{"@type":"Person","@id":"https:\/\/dev.sapien.com\/blog\/author\/alexander-riedel\/#author","url":"https:\/\/dev.sapien.com\/blog\/author\/alexander-riedel\/","name":"Alex Riedel"},{"@type":"WebPage","@id":"https:\/\/dev.sapien.com\/blog\/2009\/09\/02\/powershell-hosting-and-myinvocation\/#webpage","url":"https:\/\/dev.sapien.com\/blog\/2009\/09\/02\/powershell-hosting-and-myinvocation\/","name":"PowerShell hosting and $MyInvocation - DEV SAPIEN Blog","description":"Many PowerShell users utilize code as shown below to determine where the running script resides: function get-scriptdirectory{ $invocation=(get-variable MyInvocation -Scope 1).Value Split-Path $invocation.myinvocation.path} Unfortunately, if the script runs inside a custom host, such as PrimalScript's internal host, for a good number of debuggers and the SAPIEN PowerShell host from the PrimalForms packager, the MyInvocation variable","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/dev.sapien.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/dev.sapien.com\/blog\/2009\/09\/02\/powershell-hosting-and-myinvocation\/#breadcrumblist"},"author":{"@id":"https:\/\/dev.sapien.com\/blog\/author\/alexander-riedel\/#author"},"creator":{"@id":"https:\/\/dev.sapien.com\/blog\/author\/alexander-riedel\/#author"},"datePublished":"2009-09-02T14:57:44-07:00","dateModified":"2009-09-02T14:58:30-07:00"},{"@type":"WebSite","@id":"https:\/\/dev.sapien.com\/blog\/#website","url":"https:\/\/dev.sapien.com\/blog\/","name":"DEV SAPIEN Blog","description":"Tools for IT Success","inLanguage":"en-US","publisher":{"@id":"https:\/\/dev.sapien.com\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"DEV SAPIEN Blog - Tools for IT Success","og:type":"article","og:title":"PowerShell hosting and $MyInvocation - DEV SAPIEN Blog","og:description":"Many PowerShell users utilize code as shown below to determine where the running script resides: function get-scriptdirectory{ $invocation=(get-variable MyInvocation -Scope 1).Value Split-Path $invocation.myinvocation.path} Unfortunately, if the script runs inside a custom host, such as PrimalScript's internal host, for a good number of debuggers and the SAPIEN PowerShell host from the PrimalForms packager, the MyInvocation variable","og:url":"https:\/\/dev.sapien.com\/blog\/2009\/09\/02\/powershell-hosting-and-myinvocation\/","article:published_time":"2009-09-02T22:57:44+00:00","article:modified_time":"2009-09-02T22:58:30+00:00","twitter:card":"summary_large_image","twitter:title":"PowerShell hosting and $MyInvocation - DEV SAPIEN Blog","twitter:description":"Many PowerShell users utilize code as shown below to determine where the running script resides: function get-scriptdirectory{ $invocation=(get-variable MyInvocation -Scope 1).Value Split-Path $invocation.myinvocation.path} Unfortunately, if the script runs inside a custom host, such as PrimalScript's internal host, for a good number of debuggers and the SAPIEN PowerShell host from the PrimalForms packager, the MyInvocation variable"},"aioseo_meta_data":{"post_id":"1628","title":null,"description":null,"keywords":null,"keyphrases":null,"focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_custom_url":null,"og_image_custom_fields":null,"og_image_url":null,"og_image_width":null,"og_image_height":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_image_url":null,"twitter_title":null,"twitter_description":null,"schema_type":"default","schema_type_options":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null,"created":"2026-08-28 14:55:39","updated":"2026-08-28 14:55:39"},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/dev.sapien.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/dev.sapien.com\/blog\/topics\/windows-powershell\/\" title=\"Windows PowerShell\">Windows PowerShell<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tPowerShell hosting and $MyInvocation\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/dev.sapien.com\/blog"},{"label":"Windows PowerShell","link":"https:\/\/dev.sapien.com\/blog\/topics\/windows-powershell\/"},{"label":"PowerShell hosting and $MyInvocation","link":"https:\/\/dev.sapien.com\/blog\/2009\/09\/02\/powershell-hosting-and-myinvocation\/"}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1628","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=1628"}],"version-history":[{"count":7,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1628\/revisions"}],"predecessor-version":[{"id":1716,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1628\/revisions\/1716"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=1628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=1628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=1628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}