{"id":17826,"date":"2008-12-23T07:45:34","date_gmt":"2008-12-23T15:45:34","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/powershell\/?p=17826"},"modified":"2019-06-07T07:48:49","modified_gmt":"2019-06-07T15:48:49","slug":"windows-powershell-ctp2-to-ctp3-conversion-guide","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/windows-powershell-ctp2-to-ctp3-conversion-guide\/","title":{"rendered":"Windows PowerShell CTP2 to CTP3 Conversion Guide"},"content":{"rendered":"<p><P>I write a lot of scripts, and, since I blog some of what I write, my home computer has been running Windows PowerShell CTP2 since it came out.&nbsp; Since CTP3 has a number of changes from CTP2, I&#8217;ve got to update my home script library to work with CTP3.&nbsp; While this guide might not have every change that happened in between CTP2 and CTP3 (because I script with most, but not all, of PowerShell), I hope that this record of conversion steps helps everyone else moving from Windows PowerShell CTP2 to CTP3.&nbsp; <A href=\"http:\/\/www.nivot.org\/2008\/12\/23\/PowerShell20CTP3HasArrived.aspx\" mce_href=\"http:\/\/www.nivot.org\/2008\/12\/23\/PowerShell20CTP3HasArrived.aspx\">Oisin Grehan has posted a fairly complete list of cmdlet name changes on his blog, Nivot Ink<\/A>.&nbsp; This document only details what I&#8217;ve had to do to update my own scripts from CTP2 to CTP3, but not everything that has changed between the two releases.<\/P>\n<P>Here&#8217;s a list of all of the changes I&#8217;ve had to make to my personal script collection so far.<\/P>\n<OL>\n<LI>Graphical PowerShell is now the PowerShell Integrated Scripting Environment, the profile name for this user interface has changed.&nbsp; If you made a profile in Graphical PowerShell, simply rename the file from Microsoft.GPowerShell_profile.ps1 to Microsoft.PowerShellISE_profile.ps1 <\/LI>\n<LI>Modules were in a packages directory, and are now in a modules directory, rename the packages directory (if you have one) to modules <\/LI>\n<LI>You&#8217;re Importing Modules, not adding them, so Add-Module is now renamed Import-Module <\/LI>\n<LI>Script Cmdlets are now technically called &#8220;advanced functions&#8221;, and so the cmdlet keyword has been removed from the language.&nbsp; If you built a script cmdlet by starting with the word cmdlet (e.g. cmdlet foo), rename cmdlet to function. If you simply designated a function to be a script cmdlet by adding the cmdlet keyword before the parameter declaration, remove it.&nbsp; If you happened to have a default parameter set, then you&#8217;ll have to specify the default parameter set by adding a CmdletBinding attribute: \n<P><STRONG>CTP2:<\/STRONG><\/P>\n<P><BR>cmdlet ` <BR>&nbsp; -DefaultParameterSet Type `<\/P>\n<P><STRONG>CTP3<\/STRONG>:<\/P>\n<P>[CmdletBinding(DefaultParameterSetName=&#8217;Type&#8217;)]<\/P>\n<P>Note that it&#8217;s DefaultParameterSetName, not DefaultParameterSet.&nbsp; In my script collection, this was the most tedious thing to change out.<\/P><\/LI>\n<LI>The New-PSEvent cmdlet is now New-Event <\/LI>\n<LI>The New API to invoke PowerShell from within C# (this was used in my WPF post series) underwent a number of transitions.&nbsp; First and foremost, because things in the top-level namespace were easier to find, we&#8217;ve moved the class from the System.Management.Automation.Runspaces namespace to System.Management.Automation.&nbsp; Also, since there were a lot of different overloads for Creating PowerShell API instances and for adding parameters, we&#8217;ve consolidated the code significantly.&nbsp; As in CTP2, Adding parameters or commands returns the Powershell object, so you can chain a few commands together rather than storing a variable. Oisin pointed out that there are type accelerators for the PowerShell API, Runspace, and RunspaceFactory<BR><BR><STRONG>CTP2: <BR><BR><\/STRONG>$psCmd = [Management.Automation.Runspaces.PowerShell]::Create(<EM>Command<\/EM>, <EM>IsScript)<\/EM> <BR><BR><STRONG>CTP3: <BR><BR><\/STRONG>[PowerShell]::Create().AddScript(&#8216;1..100&#8217;).Invoke() <BR><BR>or: <BR><BR>[Management.Automation.PowerShell]::Create().AddCommand(&#8216;Get-Command&#8217;).AddParameter(&#8220;Verb&#8221;,&#8221;Get&#8221;).AddParameter(&#8220;Syntax&#8221;).Invoke() <\/LI>\n<LI>The PowerShell API also no longer has GetRunspace\/SetRunspace or GetRunspacePool\/SetRunspacePool methods.&nbsp; Instead, it has a runspace property and a runspace pool property.&nbsp; Here&#8217;s what a hello world in WPF would look like in CTP2 vs CTP3 <BR><BR>CTP2: <BR># Create a runspace to run Hello World <BR>$rs = [Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace() <BR>$rs.ApartmentState, $rs.ThreadOptions = \u201cSTA\u201d, \u201cReuseThread\u201d <BR>$rs.Open() <BR># Reference the WPF assemblies <BR>$psCmd = {Add-Type}.GetPowerShell() <BR>$psCmd.SetRunspace($rs) <BR>$psCmd.AddParameter(&#8220;AssemblyName&#8221;, &#8220;PresentationCore&#8221;).Invoke() <BR>$psCmd.Command.Clear() <BR>$psCmd = $psCmd.AddCommand(&#8220;Add-Type&#8221;) <BR>$psCmd.AddParameter(&#8220;AssemblyName&#8221;, &#8220;PresentationFramework&#8221;).Invoke() <BR>$psCmd.Command.Clear() <BR>$psCmd = $psCmd.AddCommand(&#8220;Add-Type&#8221;) <BR>$psCmd.AddParameter(&#8220;AssemblyName&#8221;, &#8220;WindowsBase&#8221;).Invoke() <BR>$sb = $executionContext.InvokeCommand.NewScriptBlock( <BR>(Join-Path $pwd &#8220;HelloWorld.ps1&#8221;) <BR>) <BR>$psCmd = $sb.GetPowerShell() <BR>$psCmd.SetRunspace($rs) <BR>$null = $psCmd.BeginInvoke() <BR><BR>CTP3: \n<P># Create a runspace to run Hello World <BR>$rs = [RunspaceFactory]::CreateRunspace() <BR>$rs.ApartmentState, $rs.ThreadOptions = \u201cSTA\u201d, \u201cReuseThread\u201d <BR>$rs.Open() <BR>$psCmd = {Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase}.GetPowerShell() <BR>$psCmd.Runspace = $rs <BR>$psCmd.Invoke() <BR>$psCmd.Commands.Clear() <BR>$psCmd.AddScript({ <BR>&nbsp;&nbsp;&nbsp; $window = New-Object Windows.Window <BR>&nbsp;&nbsp;&nbsp; $window.Title = $window.Content = \u201cHello World.&nbsp; Check out PowerShell and WPF Together.\u201d <BR>&nbsp;&nbsp;&nbsp; $window.FontSize = &#8217;24&#8217; <BR>&nbsp;&nbsp;&nbsp; $window.SizeToContent = \u201cWidthAndHeight\u201d <BR>&nbsp;&nbsp;&nbsp; $window.add_MouseDoubleClick({$this.Close()}) <BR>&nbsp;&nbsp;&nbsp; $null = $window.ShowDialog() <BR>}).BeginInvoke()<\/P><\/LI>\n<LI>$commandLineParameters, a variable that held what named parameters were bound to the current function, is now $psBoundParameters <\/LI>\n<LI>Get-PSCallStack no longer returns strings.&nbsp; It now returns a more detailed command object.&nbsp; The value of $myInvocation at each level of the callstack is stored in a property called InvocationInfo <\/LI>\n<LI>The Exports property of a PSModuleInfo (the class returned from Get-Module) is gone.&nbsp; Exports was a bunch of strings indicating the commands exported from a module.&nbsp; The exports has been replaced by several dictionaries containing more detailed export information: ExportedAliases, ExportedCmdlets, ExportedFunctions, and ExportedVariables. <\/LI><\/OL>\n<P>Once again, this is only the changes that I&#8217;ve made to my personal script collection at home.&nbsp; This post doesn&#8217;t contain all that is new or cool with CTP3 (and there&#8217;s a lot of new coolness in CTP3), but it does contain the changes that broke my CTP2 scripts that I am aware of. I will update as I make more changes, and, if you&#8217;ve had any issues converting your CTP2 scripts to CTP3, please post a comment to this blog and I&#8217;ll update the conversion guide.<\/P>\n<P>Hope this Helps,<\/P>\n<P>James Brundage [MSFT]<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I write a lot of scripts, and, since I blog some of what I write, my home computer has been running Windows PowerShell CTP2 since it came out.&nbsp; Since CTP3 has a number of changes from CTP2, I&#8217;ve got to update my home script library to work with CTP3.&nbsp; While this guide might not have [&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":[94,136,137,179,201,217,270,302,360],"class_list":["post-17826","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-add-module","tag-ctp2","tag-ctp3","tag-get-pscallstack","tag-import-module","tag-modules","tag-powershell-v2","tag-scriptcmdlet","tag-wpf"],"acf":[],"blog_post_summary":"<p>I write a lot of scripts, and, since I blog some of what I write, my home computer has been running Windows PowerShell CTP2 since it came out.&nbsp; Since CTP3 has a number of changes from CTP2, I&#8217;ve got to update my home script library to work with CTP3.&nbsp; While this guide might not have [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/17826","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=17826"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/17826\/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=17826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=17826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=17826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}