{"id":10841,"date":"2006-04-25T12:17:56","date_gmt":"2006-04-25T12:17:56","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/04\/25\/using-culture-culture-culture-script-scriptblock\/"},"modified":"2019-02-18T13:25:04","modified_gmt":"2019-02-18T20:25:04","slug":"using-culture-culture-culture-script-scriptblock","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/using-culture-culture-culture-script-scriptblock\/","title":{"rendered":"Using-Culture -Culture culture -Script {scriptblock}"},"content":{"rendered":"<p><font face=\"Courier New\">One of the traditional challenges with scripting is dealing with different CULTURES.&nbsp; Imagine the case where you are writing a script and you&#8217;ll have to parse datetime string&nbsp;from different cultures.&nbsp; If it was just one culture, you could set the process culture and be done with it.&nbsp; Here is a function that allows you to run a portion of a script in a different culture:<\/font><\/p>\n<p><font face=\"Courier New\">Function Using-Culture (<br \/>[System.Globalization.CultureInfo]$culture = (throw &#8220;USAGE: Using-Culture -Culture culture -Script {scriptblock}&#8221;),<br \/>[ScriptBlock]$script= (throw &#8220;USAGE: Using-Culture -Culture culture -Script {scriptblock}&#8221;))<br \/>{<br \/>&nbsp;&nbsp;&nbsp; $OldCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture<br \/>&nbsp;&nbsp;&nbsp; <\/font><font face=\"Courier New\">trap&nbsp;<br \/>&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture<br \/>&nbsp;&nbsp;&nbsp; }<\/font><font face=\"Courier New\"><br \/>&nbsp;&nbsp;&nbsp; [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture<br \/>&nbsp;&nbsp;&nbsp; Invoke-Command $script<br \/>&nbsp;&nbsp;&nbsp; [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture<br \/>}<\/font><\/p>\n<p>Here is an example of it working:<\/p>\n<p><font face=\"Courier New\">[332:]MSH&gt; using-culture ar-IQ {get-date}<\/font><\/p>\n<p><font face=\"Courier New\">30 \u062a\u0634\u0631\u064a\u0646&nbsp;\u0627\u0644\u062b\u0627\u0646\u064a, 2005 09:01:38 \u0635<\/font><\/p>\n<p><font face=\"Courier New\">[332:]MSH&gt; using-culture ar-IQ {$global:d=[DateTIme]::Parse(&#8220;30 \u062a\u0634\u0631\u064a\u0646&nbsp;\u0627\u0644\u062b\u0627\u0646\u064a, 2005 09:01:38 \u0635&#8221;)}<br \/>[332:]MSH&gt; $d<\/font><\/p>\n<p><font face=\"Courier New\">Wednesday, November 30, 2005 9:01:38 AM<\/font><\/p>\n<p><font face=\"Courier New\">[332:]MSH&gt; using-culture de-de {get-date}<\/font><\/p>\n<p><font face=\"Courier New\">Mittwoch, 30. November 2005 09:02:29<br \/>[332:]MSH&gt; using-culture de-de {$global:d=[DateTIme]::Parse(&#8220;Mittwoch, 30. November 2005 09:02:29&#8221;)}<br \/>[332:]MSH&gt; $d<\/font><\/p>\n<p><font face=\"Courier New\">Wednesday, November 30, 2005 9:02:29 AM<\/font><\/p>\n<p><font face=\"Courier New\"><\/font>&nbsp;<\/p>\n<p><font face=\"Courier New\">The great thing about this approach is that you can put any code you want into the script block.&nbsp; It can be a single cmd, a pipeline or a full script.<\/font><\/p>\n<p><font face=\"Courier New\">Enjoy!<\/font><\/p>\n<p><font face=\"Courier New\">Jeffrey P. Snover [MSFT]<\/font><\/p>\n<p>[<i>Edit: Monad has now been renamed to Windows PowerShell.  This script or discussion may require slight adjustments before it applies directly to newer builds.<\/i>]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the traditional challenges with scripting is dealing with different CULTURES.&nbsp; Imagine the case where you are writing a script and you&#8217;ll have to parse datetime string&nbsp;from different cultures.&nbsp; If it was just one culture, you could set the process culture and be done with it.&nbsp; Here is a function that allows you to [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10841","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>One of the traditional challenges with scripting is dealing with different CULTURES.&nbsp; Imagine the case where you are writing a script and you&#8217;ll have to parse datetime string&nbsp;from different cultures.&nbsp; If it was just one culture, you could set the process culture and be done with it.&nbsp; Here is a function that allows you to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10841","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=10841"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10841\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=10841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=10841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=10841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}