{"id":305731,"date":"2021-07-22T21:00:00","date_gmt":"2021-07-22T18:00:00","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/powershell-7-cross-platform-best-practices-cloudsavvy-it\/"},"modified":"2021-07-22T21:00:00","modified_gmt":"2021-07-22T18:00:00","slug":"powershell-7-cross-platform-best-practices-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/powershell-7-cross-platform-best-practices-cloudsavvy-it\/","title":{"rendered":"#PowerShell 7 Cross-Platform Best Practices \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a30575e08ea2\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a30575e08ea2\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/powershell-7-cross-platform-best-practices-cloudsavvy-it\/#Use_Lowercase_Directory_Names\" >Use Lowercase Directory Names<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/powershell-7-cross-platform-best-practices-cloudsavvy-it\/#Dont_Use_Aliasing\" >Don\u2019t Use Aliasing<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/powershell-7-cross-platform-best-practices-cloudsavvy-it\/#Use_a_Switch_Statement_to_Determine_Operating_System\" >Use a Switch Statement to Determine Operating System<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#PowerShell 7 Cross-Platform Best Practices \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-4374\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/03\/23e4a5a4.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/03\/23e4a5a4.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/03\/23e4a5a4.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Powershell logo\" width=\"1400\" height=\"578\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>When PowerShell was first released, the goal was to bring powerful <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">script<\/a>ing tools to Windows, which typically had GUI-based administration at the time. Since then, PowerShell has grown to become one of the most popular scripting languages.<\/p>\n<p>One of the biggest contributors to this rise in popularity is that the PowerShell team at Microsoft made it <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/click.linksynergy.com\/deeplink?id=2QzUaswX1as&amp;mid=24542&amp;u1=csit\/5037&amp;murl=https%3A%2F%2Fdevblogs.microsoft.com%2Fpowershell%2Fpowershell-on-linux-and-open-source-2%2F\">open-source in 2016<\/a>, introducing PowerShell Core for PowerShell version 6. However, that caused some unexpected issues, like having to support compatibility between version 5.1 (Windows PowerShell) and version 6.2 (PowerShell Core), or having the same cmdlets doing slightly different things on different versions of .NET.<\/p>\n<p>To settle that, the PowerShell team announced they were retiring PowerShell 5.1 and that the next version after 6.2 would be <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/click.linksynergy.com\/deeplink?id=2QzUaswX1as&amp;mid=24542&amp;u1=csit\/5037&amp;murl=https%3A%2F%2Fdevblogs.microsoft.com%2Fpowershell%2Fthe-next-release-of-powershell-powershell-7%2F\">PowerShell 7<\/a>, which would aim to replace Windows PowerShell on Windows Operating systems without sacrificing any compatibility when running on Mac or Linux.<\/p>\n<p>PowerShell 7 <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/click.linksynergy.com\/deeplink?id=2QzUaswX1as&amp;mid=24542&amp;u1=csit\/5037&amp;murl=https%3A%2F%2Fdevblogs.microsoft.com%2Fpowershell%2Fannouncing-powershell-7-0%2F\">went GA in March<\/a> of last year, and while the cmdlets themselves run on any operating system, there are still some best practices to follow to make sure your PowerShell scripts run smoothly no matter where they are running.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Use_Lowercase_Directory_Names\"><\/span>Use Lowercase Directory Names<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In the Windows Operating system, the <code>\\<\/code> or backslash character is used to note nested directories in a file system, while in Linux and macOS, the <code>\/<\/code> or forward-slash character does it instead and the backslash is used as an escape character. (If you ever have trouble keeping these two straight, just imagine it tipping over. Backslash falls back, forward slash falls forward)<\/p>\n<p>This can make for some challenges when scripting in most other languages, but PowerShell 7 will translate either into a directory object and doesn\u2019t really care about the characters used to split the path. One area where you can get into trouble, however, is that Linux and macOS file systems care about capitalization, and will treat directory names in different cases as different directories.<\/p>\n<p>The best practice here is to keep directory names in lowercase, no matter what path you use. If you need to, you can pass in the path as a string and call the <code>ToLower()<\/code> method on it in your code, but this will only work if the directory name is already in lower case.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"imgchk9 alignnone wp-image-5039 size-full\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/06\/235920b3-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\" kKeep directory names in lowercase, no matter what path you use.\" width=\"1151\" height=\"314\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Dont_Use_Aliasing\"><\/span>Don\u2019t Use Aliasing<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Aliasing can be a great way to shorten the amount of typing you have to do when running PowerShell from a console. It\u2019s much easier to type <code>ls<\/code> or <code>cgi<\/code> than <code>Get-ChildItem<\/code> even with tab completion. That being said, it\u2019s rarely a good idea to use aliases in scripts because it makes the script dependent on having that alias set wherever it runs.<\/p>\n<p>That\u2019s especially true for Linux and macOS systems. On those platforms, instead of using an alias to run another PowerShell command, it runs the native command, meaning that the return object is going to be very different, and that will throw off the rest of the script.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"imgchk9 alignnone wp-image-5040 size-full\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/06\/8cdf9721.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"ls and Get-ChildItem running on Windows 10 vs Ubuntu Linux\" width=\"1114\" height=\"491\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p><code>ls<\/code> and <code>Get-ChildItem<\/code> running on Windows 10 vs Ubuntu Linux.<\/p>\n<p>Some commands like <code>ls<\/code>\u00a0might not change too much, but with others like the <code>date<\/code> command, PowerShell returns a completely different object from the native Linux command, which will throw off anything that is depending on the output later on in the code. Even if you just need the string value in a PowerShell script, use PowerShell to get it.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"imgchk9 alignnone wp-image-5041 size-full\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/06\/1952a2db.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"date returns a DateTime object on Windows and a String object on Linux.\" width=\"1151\" height=\"294\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p><code>date<\/code> returns a DateTime object on Windows and a String object on Linux.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Use_a_Switch_Statement_to_Determine_Operating_System\"><\/span>Use a Switch Statement to Determine Operating System<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Sometimes, you have a dependency that you can\u2019t script around, no matter what kinds of tricks you use. In those cases, PowerShell 7 has a few reserved flags that will return <code>true<\/code> or <code>false<\/code> depending on what operating system you are running from. For example, <code>$IsMacOS<\/code> will return <code>True<\/code> on macOS and <code>False<\/code> on any other operating system, and <code>$IsLinux<\/code> will return <code>True<\/code> no matter which Linux distribution you are on.<\/p>\n<p>There is also an <code>$IsWindows<\/code> flag, but with one big caveat: It only returns <code>True<\/code> when running PowerShell Core or PowerShell 7 on Windows. It was never ported back to Windows PowerShell, so it will just throw an error when running\u00a0there.<\/p>\n<p>Tyler Leonhardt from the PowerShell team at Microsoft came up with a great workaround for determining which OS you are on when running cross-platform, which is the switch statement he describes <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/twitter.com\/TylerLeonhardt\/status\/1220165995710443520\">here<\/a>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"imgchk9 alignnone wp-image-5042 size-full\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/06\/e8b2ac4e.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Great workaround for determining which OS you are on when running cross-platform.\" width=\"1151\" height=\"274\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Now you should have an idea of a few ways to write your PowerShell 7 scripts and modules so that they can run a little more smoothly cross-platform. Try them out in your scripts today!\n<\/p><\/div>\n<p><script async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/5037\/powershell-7-cross-platform-best-practices\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#PowerShell 7 Cross-Platform Best Practices \u2013 CloudSavvy IT&#8221; When PowerShell was first released, the goal was to bring powerful scripting tools to Windows, which typically had GUI-based administration at the time. Since then, PowerShell has grown to become one of the most popular scripting languages. One of the biggest contributors to this rise in popularity&#8230;<\/p>\n","protected":false},"author":1,"featured_media":305732,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/03\/23e4a5a4.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-305731","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/305731","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=305731"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/305731\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/305732"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=305731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=305731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=305731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}