{"id":51343,"date":"2010-02-10T00:01:00","date_gmt":"2010-02-10T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/02\/10\/hey-scripting-guy-a-scripting-overview-of-windows-powershell-2-0-part-1\/"},"modified":"2010-02-10T00:01:00","modified_gmt":"2010-02-10T00:01:00","slug":"hey-scripting-guy-a-scripting-overview-of-windows-powershell-2-0-part-1","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-a-scripting-overview-of-windows-powershell-2-0-part-1\/","title":{"rendered":"Hey, Scripting Guy! A Scripting Overview of Windows PowerShell 2.0 (Part 1)"},"content":{"rendered":"<p><span><span><span><span><span><span><span><span><span><span><font size=\"5\" face=\"Calibri\"><\/font><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/p>\n<h2><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a><\/h2>\n<p>(Note: Today&#8217;s Hey, Scripting Guy! Blog post is an edited excerpt from a forthcoming SAPIEN Press book authored by Don Jones and Jeffery Hicks. Don has published more than 30 books about IT pro topics; you can also find him in <em>TechNet Magazine<\/em>, on ScriptingAnswers.com, and on concentratedtech.com. Jeffery has authored or co-authored several books in the SAPIEN TFM series; he is a Windows PowerShell MVP, a columnist and contributing editor for Redmondmag.com, and a columnist for MCPMag.com. Find out more about Jeffery at <a href=\"http:\/\/jdhitsolutions.com\/blog\">http:\/\/jdhitsolutions.com\/blog<\/a>.<\/p>\n<p>Today is part 1 of 2. Part 2 will be published tomorrow. Thanks, Don and Jeffery!)<\/p>\n<p>&#8212;&#8212;&#8212;-<\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">With many shells&mdash;particularly some *nix shells&mdash;using the shell interactively is a very different experience than scripting with the shell. Typically, shells offer a complete scripting language that is <i>only<\/i> available when you&rsquo;re running a script. Not so with Windows PowerShell: The shell behaves exactly the same, and offers exactly the same features and functionality, whether you&rsquo;re writing a script or using the shell interactively. In fact, a Windows PowerShell script is a true script&mdash;simply a text file listing the things you&rsquo;d type interactively. The only reason to write a script is because you&rsquo;re tired of typing those things interactively and want to be able to run them again and again with minimal effort. <\/p>\n<p><\/font><\/span><\/p>\n<h2><span><font size=\"4\" face=\"Arial Black\">Script Files<\/font><\/span><\/p>\n<\/h2>\n<p class=\"MsoNormal\"><span><font size=\"3\">Windows PowerShell recognizes the .ps1 file name extension as a Windows PowerShell script. Notice the &ldquo;1&rdquo; in there? That indicates a script designed to work with Windows PowerShell version 1; future versions of Windows PowerShell will presumably be able to use that as an indicator for backward-compatibility. Script files are simple text files; you can edit them with Notepad or any other text editor. In fact, by default, Windows associates the .ps1 file name extension with Notepad, not Windows PowerShell, so double-clicking a script file opens it in Notepad rather than executing it in Windows PowerShell. Of course, we&rsquo;re a bit biased against Notepad as a script editor: Notepad was certainly never designed for that task, and better options exist. We&rsquo;re obviously keen on SAPIEN PrimalScript (www.primalscript.com) because it offers a full visual development environment with Windows PowerShell&ndash;specific support, such as the ability to package a Windows PowerShell script in a stand-alone executable that runs under alternate credentials.<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">Note that Windows PowerShell 2.0 continues to use the .ps1 file name extension; think of this extension as &ldquo;version 1 of the script file format,&rdquo; rather than having a direct relation to version 2.0 of the shell. In fact, version 2.0 should run most version 1.0 scripts unchanged.<\/p>\n<p><\/font><\/span><\/p>\n<h2><span><font size=\"4\" face=\"Arial Black\">Profiles<\/font><\/span><\/p>\n<\/h2>\n<p class=\"MsoNormal\"><span><font size=\"3\">Windows PowerShell supports four special scripts called <i>profiles<\/i>. These scripts are physically identical to any other script; what makes them special is that Windows PowerShell looks for them when it starts and, if it finds them, executes them. Think of them as a sort of &ldquo;auto-run&rdquo; set of scripts, allowing you to define custom aliases, functions, and so forth. For example, by defining custom aliases in your profile, those aliases will be defined every time Windows PowerShell runs, making your aliases available to you anytime you&rsquo;re using the shell.<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">Windows PowerShell looks for profiles using a specific path and file name. It looks for them&mdash;and executes them, if they&rsquo;re present&mdash;in the following order:<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"BulletedList\"><span><span><font size=\"3\">&middot;<\/font><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><span><font size=\"3\">%windir%system32WindowsPowerShellv1.0profile.ps1<br \/>This applies to all users and to all shells.<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"BulletedList\"><span><span><font size=\"3\">&middot;<\/font><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><span><font size=\"3\">%windirsystem32WindowsPowerShellv1.0Microsoft.PowerShell_profile.ps1<br \/>This applies to all users but only to the PowerShell.exe shell.<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"BulletedList\"><span><span><font size=\"3\">&middot;<\/font><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><span><font size=\"3\">%UserDocuments%WindowsPowerShellprofile.ps1<br \/>This applies to the current user but affects all shells.<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"BulletedList\"><span><span><font size=\"3\">&middot;<\/font><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><span><font size=\"3\">%UserDocuments%WindowsPowerShellMicrosoft.PowerShell_profile.ps1<br \/>This applies to the current user and only to the PowerShell.exe shell.<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNoteHeading\"><span><strong><font size=\"3\">By the Way&hellip;<\/p>\n<p><\/font><\/strong><\/span><\/p>\n<p class=\"NoteBody\"><span><font size=\"3\">%UserDocuments% isn&rsquo;t a valid environment variable. We&rsquo;re using it to represent the user&rsquo;s &ldquo;Documents&rdquo; folder. On Windows XP, for example, this would be under %UserProfile%My Documents; on Windows Vista, it&rsquo;s under %UserProfile%Documents.<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">Notice the references to &ldquo;all shells.&rdquo; We&rsquo;re primarily working with the PowerShell.exe shell. However, other shells exist: The Exchange Management Shell (which ships with Exchange Server 2007) is a different shell. If you have things you want defined in all the shells you use&mdash;such as custom aliases&mdash;you&rsquo;d put them in one of the &ldquo;all shells&rdquo; profiles. <\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNoteHeading\"><span><strong><font size=\"3\">A Note on Shells<\/p>\n<p><\/font><\/strong><\/span><\/p>\n<p class=\"NoteBody\"><span><font size=\"3\">Note that you don&rsquo;t <i>have<\/i> to use custom shells. For example, you don&rsquo;t need to use the Exchange Management Shell to manage Exchange Server 2007. Instead, you could simply add the Exchange snap-in to PowerShell.exe, using <b>Add-PSSnapIn<\/b>. Doing so would give you access to the Exchange cmdlets from the PowerShell.exe shell. This trick allows you to create a shell environment that has <i>all<\/i> the cmdlets you need to manage <i>all<\/i> your Windows PowerShell&ndash;manageable products.<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\">None of these profile files are created by default. You should also remember that these are just Windows PowerShell scripts, so they won&rsquo;t run unless they meet your shell&rsquo;s execution policy. In other words, your profiles need to be signed if your execution policy is <b>AllSigned<\/b>.<\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\"><\/font><\/span>&nbsp;<\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\"><strong>See you tomorrow for part 2!<\/strong><\/font><\/span><\/p>\n<p class=\"MsoNormal\"><span><font size=\"3\"><\/p>\n<p><\/font><\/span>&nbsp;\n<\/span>&lt;?xml:namespace prefix = o ns = &#8220;urn:schemas-microsoft-com:office:office&#8221; \/&gt;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(Note: Today&#8217;s Hey, Scripting Guy! Blog post is an edited excerpt from a forthcoming SAPIEN Press book authored by Don Jones and Jeffery Hicks. Don has published more than 30 books about IT pro topics; you can also find him in TechNet Magazine, on ScriptingAnswers.com, and on concentratedtech.com. Jeffery has authored or co-authored several books [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[64,51,56,65,3,4,45],"class_list":["post-51343","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-don-jones","tag-getting-started","tag-guest-blogger","tag-jeffery-hicks","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>(Note: Today&#8217;s Hey, Scripting Guy! Blog post is an edited excerpt from a forthcoming SAPIEN Press book authored by Don Jones and Jeffery Hicks. Don has published more than 30 books about IT pro topics; you can also find him in TechNet Magazine, on ScriptingAnswers.com, and on concentratedtech.com. Jeffery has authored or co-authored several books [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51343","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=51343"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51343\/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=51343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=51343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=51343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}