{"id":2613,"date":"2012-12-05T13:04:27","date_gmt":"2012-12-05T18:04:27","guid":{"rendered":"http:\/\/jdhitsolutions.com\/blog\/?p=2613"},"modified":"2012-12-06T08:12:11","modified_gmt":"2012-12-06T13:12:11","slug":"create-powershell-scripts-with-a-single-command","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2613\/create-powershell-scripts-with-a-single-command\/","title":{"rendered":"Create PowerShell Scripts with a Single Command"},"content":{"rendered":"<p><a href=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/talkbubble.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/talkbubble-150x150.png\" alt=\"\" title=\"talkbubble\" width=\"150\" height=\"150\" class=\"alignleft size-thumbnail wp-image-1688\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/talkbubble-150x150.png 150w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/talkbubble-198x198.png 198w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a>One of the drawbacks to using a PowerShell script or function is that you have to write it. For many IT Pros, especially those new to PowerShell, it can be difficult to know where to start. I think more people would write their own tools if there was an easy way to automatically write them. So here's my solution. I wrote a function called New-PSCommand that is intended as a rapid development tool for a PowerShell advanced function. It should work in either PowerShell v2 or v3.<\/p>\n<p>Here's the function, although I'm not going to spend much time explaining how it works but rather how to use it.<\/p>\n<p><code lang=\"PowerShell\"><br \/>\nFunction New-PSCommand {<br \/>\n#comment base help goes here<\/p>\n<p>[cmdletbinding()]<\/p>\n<p>Param(<br \/>\n[Parameter(Mandatory=$True,HelpMessage=\"Enter the name of your new command\")]<br \/>\n[ValidateNotNullorEmpty()]<br \/>\n[string]$Name,<br \/>\n[ValidateScript({<br \/>\n#test if using a hashtable or a v3 [ordered] hash table<br \/>\n($_ -is [hashtable]) -OR ($_ -is [System.Collections.Specialized.OrderedDictionary])<br \/>\n})]<\/p>\n<p>[Alias(\"Parameters\")]<br \/>\n[object]$NewParameters,<br \/>\n[switch]$ShouldProcess,<br \/>\n[string]$Synopsis,<br \/>\n[string]$Description,<br \/>\n[string]$BeginCode,<br \/>\n[string]$ProcessCode,<br \/>\n[string]$EndCode,<br \/>\n[switch]$UseISE<br \/>\n)<\/p>\n<p>Write-Verbose \"Starting $($myinvocation.mycommand)\"<br \/>\n#add parameters<br \/>\n$myparams=\"\"<br \/>\n$helpparams=\"\"<\/p>\n<p>Write-Verbose \"Processing parameter names\"<\/p>\n<p>foreach ($k in $newparameters.keys) {<br \/>\n    Write-Verbose \"  $k\"<br \/>\n    $paramsettings = $NewParameters.item($k)<\/p>\n<p>    #process any remaining elements from the hashtable value<br \/>\n    #@{ParamName=\"type[]\",Mandatory,ValuefromPipeline,ValuefromPipelinebyPropertyName,Position}<\/p>\n<p>    if ($paramsettings.count -gt 1) {<br \/>\n       $paramtype=$paramsettings[0]<br \/>\n      if ($paramsettings[1] -is [object]) {<br \/>\n        $Mandatory = \"Mandatory=`${0},\" -f $paramsettings[1]<br \/>\n        Write-Verbose $Mandatory<br \/>\n      }<br \/>\n      if ($paramsettings[2] -is [object]) {<br \/>\n        $PipelineValue = \"ValueFromPipeline=`${0},\" -f $paramsettings[2]<br \/>\n        Write-Verbose $PipelineValue<br \/>\n      }<br \/>\n      if ($paramsettings[3] -is [object]) {<br \/>\n        $PipelineName = \"ValueFromPipelineByPropertyName=`${0}\" -f $paramsettings[3]<br \/>\n        Write-Verbose $PipelineName<br \/>\n      }<br \/>\n      if ($paramsettings[4] -is [object]) {<br \/>\n        $Position = \"Position={0},\" -f $paramsettings[4]<br \/>\n        Write-Verbose $Position<br \/>\n      }<br \/>\n    }<br \/>\n    else {<br \/>\n     #the only hash key is the parameter type<br \/>\n     $paramtype=$paramsettings<br \/>\n    }<\/p>\n<p>    $item = \"[Parameter({0}{1}{2}{3})]`n\" -f $Position,$Mandatory,$PipelineValue,$PipelineName<br \/>\n    $item +=\"[{0}]`${1}\" -f $paramtype,$k<br \/>\n    Write-Verbose \"Adding $item to myparams\"<br \/>\n    $myparams+=\"$item, `n\"<br \/>\n    $helpparams+=\".PARAMETER {0} `n`n\" -f $k<br \/>\n    #clear variables but ignore errors for those that don't exist<br \/>\n    Clear-Variable \"Position\",\"Mandatory\",\"PipelineValue\",\"PipelineName\",\"ParamSettings\" -ErrorAction SilentlyContinue<\/p>\n<p>} #foreach hash key<\/p>\n<p>#get trailing comma and remove it<br \/>\n$myparams=$myparams.Remove($myparams.lastIndexOf(\",\"))<\/p>\n<p>Write-Verbose \"Building text\"<br \/>\n$text=@\"<br \/>\nFunction $name {<br \/>\n<#\n.SYNOPSIS\n$Synopsis\n\n.DESCRIPTION\n$Description\n\n$HelpParams\n.EXAMPLE\nPS C:\\> $Name<\/p>\n<p>.NOTES<br \/>\nVersion: 0.1<br \/>\nAuthor : $env:username<\/p>\n<p>.INPUTS<\/p>\n<p>.OUTPUTS<\/p>\n<p>.LINK<br \/>\n#><\/p>\n<p>[cmdletbinding(SupportsShouldProcess=`$$ShouldProcess)]<\/p>\n<p>Param (<br \/>\n$MyParams<br \/>\n)<\/p>\n<p>Begin {<br \/>\n    Write-Verbose \"Starting `$(`$myinvocation.mycommand)\"<br \/>\n    $BeginCode<br \/>\n} #begin<\/p>\n<p>Process {<br \/>\n    $ProcessCode<br \/>\n} #process<\/p>\n<p>End {<br \/>\n    $EndCode<br \/>\n    Write-Verbose \"Ending `$(`$myinvocation.mycommand)\"<br \/>\n} #end<\/p>\n<p>} #end $name function<\/p>\n<p>\"@<\/p>\n<p>if ($UseISE -and $psise) {<br \/>\n    $newfile=$psise.CurrentPowerShellTab.Files.Add()<br \/>\n    $newfile.Editor.InsertText($Text)<br \/>\n}<br \/>\nelse {<br \/>\n    Write $Text<br \/>\n}<\/p>\n<p>Write-Verbose \"Ending $($myinvocation.mycommand)\"<\/p>\n<p>} #end New-PSCommand function<br \/>\n<\/code><\/p>\n<p>The premise of this function is to take a hash table of parameter definitions and create a new PowerShell advanced function.<\/p>\n<p><a href=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/12\/new-pscommand-help.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/12\/new-pscommand-help-300x171.png\" alt=\"\" title=\"new-pscommand-help\" width=\"300\" height=\"171\" class=\"aligncenter size-medium wp-image-2614\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/12\/new-pscommand-help-300x171.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/12\/new-pscommand-help-1024x585.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/12\/new-pscommand-help-624x356.png 624w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/12\/new-pscommand-help.png 1050w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>The hash table key is the name of your parameter and the key is its type. The only other value you need is the name for your new function. The New-PSCommand function creates a new advanced function, complete with comment-based help, and writes the text to the pipeline. That way you can either review it, pipe it to Out-File or copy it to the clipboard.<\/p>\n<p><code lang=\"PowerShell\"><br \/>\nPS C:\\>$paramhash=@{Name=\"string[]\";Test=\"switch\";Path=\"string\"}<br \/>\nPS C:\\> New-PSCommand -name \"Set-MyScript\" -Newparameters $paramhash | out-file \"c:\\scripts\\set-myscript.ps1\"<br \/>\n<\/code><\/p>\n<p>The hash table of parameters for my Set-MyScript function includes an array of strings for $Name, a string for $Path, and a switch for $Test.  Here's the output:<\/p>\n<p><code lang=\"PowerShell\"><br \/>\nFunction Set-MyScript {<br \/>\n<#\n.SYNOPSIS\n\n.DESCRIPTION\n\n.PARAMETER Path \n\n.PARAMETER Name \n\n.PARAMETER Test \n\n.EXAMPLE\nPS C:\\> Set-MyScript<\/p>\n<p>.NOTES<br \/>\nVersion: 0.1<br \/>\nAuthor : Jeff<\/p>\n<p>.INPUTS<\/p>\n<p>.OUTPUTS<\/p>\n<p>.LINK<br \/>\n#><\/p>\n<p>[cmdletbinding(SupportsShouldProcess=$False)]<\/p>\n<p>Param (<br \/>\n[Parameter()]<br \/>\n[string]$Path,<br \/>\n[Parameter()]<br \/>\n[string[]]$Name,<br \/>\n[Parameter()]<br \/>\n[switch]$Test<br \/>\n)<\/p>\n<p>Begin {<br \/>\n    Write-Verbose \"Starting $($myinvocation.mycommand)\"<\/p>\n<p>} #begin<\/p>\n<p>Process {<\/p>\n<p>} #process<\/p>\n<p>End {<\/p>\n<p>    Write-Verbose \"Ending $($myinvocation.mycommand)\"<br \/>\n} #end<\/p>\n<p>} #end Set-MyScript function<br \/>\n<\/code><\/p>\n<p>All you need to do is fill in the script with the code you want to run. New-PSCommand does all of the grunt work for you leaving you just the fun part. I also support an alternate hashtable setup if you want to specify some parameter attributes. Create a hash table using this format:<\/p>\n<p><code lang=\"PowerShell\"><br \/>\n@{ParamName=\"type[]\",Mandatory,ValuefromPipeline,ValuefromPipelinebyPropertyName,Position}<br \/>\n<\/code><\/p>\n<p>Here's an example:<br \/>\n<code lang=\"PowerShell\"><br \/>\n$h = @{Name=\"string[]\",$True,$True,$False,0;<br \/>\n  Path=\"string\",$false,$false,$false,1;<br \/>\n  Size=\"int\",$false,$false,$true;<br \/>\n  Recurse=\"switch\"<br \/>\n  }<br \/>\n<\/code><\/p>\n<p>I also let you specify commands to use in the Begin, Process and End scriptblocks. You can also define values for the help synopsis and description.<\/p>\n<p>The last little bell on this tool is that if you run it in the PowerShell ISE, you can use the -UseISE switch which will open your new script in a new file in the ISE. This means you could open a new PowerShell tab in the ISE and run commands like this:<\/p>\n<p><code lang=\"PowerShell\"><br \/>\n$hash = [ordered]@{<br \/>\n Name=\"string[]\",$True,$True,$False,0<br \/>\n Path=\"string\",$false,$false,$false,1<br \/>\n PixieDust=\"int\",$false,$false,$true<br \/>\n NoUnicorn=\"switch\"<br \/>\n}<\/p>\n<p>$begin={<br \/>\n#initialize some variables<br \/>\n$arr=@()<br \/>\n$Love=$True<br \/>\n$ring=1<br \/>\n}<\/p>\n<p>$end=\"write-host 'Finished' -foreground Green\"<br \/>\n$synopsis = \"Get magical user data\"<br \/>\n$desc = @\"<br \/>\nThis command will do something really amazing and magical. All you need to do is provide<br \/>\nthe right amount of pixie dust and shavings from a unicorn horn.<\/p>\n<p>This requires PowerShell v4 and a full moon.<br \/>\n\"@<\/p>\n<p>. C:\\scripts\\New-PSCommand.ps1<br \/>\nNew-PSCommand -Name Get-UserData -NewParameters $hash -BeginCode $begin -EndCode $end -Synopsis $synopsis -Description $desc -UseISE<br \/>\n<\/code><\/p>\n<p>By the way, I'm running PowerShell v3 so I can use a [ordered] hash table which I encourage you to use if you can. When executed, I get a new script in the ISE ready for me to finish. <\/p>\n<p><code lang=\"PowerShell\"><br \/>\nFunction Get-UserData {<br \/>\n<#\n.SYNOPSIS\nGet magical user data\n\n.DESCRIPTION\nThis command will do something really amazing and magical. All you need to do is provide\nthe right amount of pixie dust and shavings from a unicorn horn.\n\nThis requires PowerShell v4 and a full moon.\n\n.PARAMETER Name \n\n.PARAMETER Path \n\n.PARAMETER PixieDust \n\n.PARAMETER NoUnicorn \n\n\n.EXAMPLE\nPS C:\\> Get-UserData<\/p>\n<p>.NOTES<br \/>\nVersion: 0.1<br \/>\nAuthor : Jeff<\/p>\n<p>.INPUTS<\/p>\n<p>.OUTPUTS<\/p>\n<p>.LINK<br \/>\n#><\/p>\n<p>[cmdletbinding(SupportsShouldProcess=$False)]<\/p>\n<p>Param (<br \/>\n[Parameter(Position=0,Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$False)]<br \/>\n[string[]]$Name,<br \/>\n[Parameter(Position=1,Mandatory=$False,ValueFromPipeline=$False,ValueFromPipelineByPropertyName=$False)]<br \/>\n[string]$Path,<br \/>\n[Parameter(Mandatory=$False,ValueFromPipeline=$False,ValueFromPipelineByPropertyName=$True)]<br \/>\n[int]$PixieDust,<br \/>\n[Parameter()]<br \/>\n[switch]$NoUnicorn<br \/>\n)<\/p>\n<p>Begin {<br \/>\n    Write-Verbose \"Starting $($myinvocation.mycommand)\"<\/p>\n<p>#initialize some variables<br \/>\n$arr=@()<br \/>\n$Love=$True<br \/>\n$ring=1<\/p>\n<p>} #begin<\/p>\n<p>Process {<\/p>\n<p>} #process<\/p>\n<p>End {<br \/>\n    write-host 'Finished' -foreground Green<br \/>\n    Write-Verbose \"Ending $($myinvocation.mycommand)\"<br \/>\n} #end<\/p>\n<p>} #end Get-UserData function<br \/>\n<\/code><\/p>\n<p>I hope that a tool like this helps cut down on your development time. Please download <a href='http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/12\/New-PSCommand.txt' target='_blank'>New-PSCommand<\/a> and let me know what you think.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the drawbacks to using a PowerShell script or function is that you have to write it. For many IT Pros, especially those new to PowerShell, it can be difficult to know where to start. I think more people would write their own tools if there was an easy way to automatically write them&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,231,8],"tags":[224,199,232,534,540],"class_list":["post-2613","post","type-post","status-publish","format-standard","hentry","category-powershell","category-powershell-ise","category-scripting","tag-function","tag-hashtable","tag-ise","tag-powershell","tag-scripting"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Create PowerShell Scripts with a Single Command &#8226; The Lonely Administrator<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/2613\/create-powershell-scripts-with-a-single-command\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Create PowerShell Scripts with a Single Command &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"One of the drawbacks to using a PowerShell script or function is that you have to write it. For many IT Pros, especially those new to PowerShell, it can be difficult to know where to start. I think more people would write their own tools if there was an easy way to automatically write them....\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/2613\/create-powershell-scripts-with-a-single-command\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2012-12-05T18:04:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-12-06T13:12:11+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/talkbubble-150x150.png\" \/>\n<meta name=\"author\" content=\"Jeffery Hicks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:site\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeffery Hicks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2613\\\/create-powershell-scripts-with-a-single-command\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2613\\\/create-powershell-scripts-with-a-single-command\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"Create PowerShell Scripts with a Single Command\",\"datePublished\":\"2012-12-05T18:04:27+00:00\",\"dateModified\":\"2012-12-06T13:12:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2613\\\/create-powershell-scripts-with-a-single-command\\\/\"},\"wordCount\":434,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2613\\\/create-powershell-scripts-with-a-single-command\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2011\\\/10\\\/talkbubble-150x150.png\",\"keywords\":[\"Function\",\"hashtable\",\"ISE\",\"PowerShell\",\"Scripting\"],\"articleSection\":[\"PowerShell\",\"PowerShell ISE\",\"Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2613\\\/create-powershell-scripts-with-a-single-command\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2613\\\/create-powershell-scripts-with-a-single-command\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2613\\\/create-powershell-scripts-with-a-single-command\\\/\",\"name\":\"Create PowerShell Scripts with a Single Command &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2613\\\/create-powershell-scripts-with-a-single-command\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2613\\\/create-powershell-scripts-with-a-single-command\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2011\\\/10\\\/talkbubble-150x150.png\",\"datePublished\":\"2012-12-05T18:04:27+00:00\",\"dateModified\":\"2012-12-06T13:12:11+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2613\\\/create-powershell-scripts-with-a-single-command\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2613\\\/create-powershell-scripts-with-a-single-command\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2613\\\/create-powershell-scripts-with-a-single-command\\\/#primaryimage\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2011\\\/10\\\/talkbubble.png\",\"contentUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2011\\\/10\\\/talkbubble.png\",\"width\":\"198\",\"height\":\"208\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2613\\\/create-powershell-scripts-with-a-single-command\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"PowerShell\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/powershell\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Create PowerShell Scripts with a Single Command\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/\",\"name\":\"The Lonely Administrator\",\"description\":\"Practical Advice for the Automating IT Pro\",\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\",\"name\":\"Jeffery Hicks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"caption\":\"Jeffery Hicks\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Create PowerShell Scripts with a Single Command &#8226; The Lonely Administrator","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2613\/create-powershell-scripts-with-a-single-command\/","og_locale":"en_US","og_type":"article","og_title":"Create PowerShell Scripts with a Single Command &#8226; The Lonely Administrator","og_description":"One of the drawbacks to using a PowerShell script or function is that you have to write it. For many IT Pros, especially those new to PowerShell, it can be difficult to know where to start. I think more people would write their own tools if there was an easy way to automatically write them....","og_url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2613\/create-powershell-scripts-with-a-single-command\/","og_site_name":"The Lonely Administrator","article_published_time":"2012-12-05T18:04:27+00:00","article_modified_time":"2012-12-06T13:12:11+00:00","og_image":[{"url":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/talkbubble-150x150.png","type":"","width":"","height":""}],"author":"Jeffery Hicks","twitter_card":"summary_large_image","twitter_creator":"@JeffHicks","twitter_site":"@JeffHicks","twitter_misc":{"Written by":"Jeffery Hicks","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2613\/create-powershell-scripts-with-a-single-command\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2613\/create-powershell-scripts-with-a-single-command\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"Create PowerShell Scripts with a Single Command","datePublished":"2012-12-05T18:04:27+00:00","dateModified":"2012-12-06T13:12:11+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2613\/create-powershell-scripts-with-a-single-command\/"},"wordCount":434,"commentCount":2,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2613\/create-powershell-scripts-with-a-single-command\/#primaryimage"},"thumbnailUrl":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/talkbubble-150x150.png","keywords":["Function","hashtable","ISE","PowerShell","Scripting"],"articleSection":["PowerShell","PowerShell ISE","Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/2613\/create-powershell-scripts-with-a-single-command\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2613\/create-powershell-scripts-with-a-single-command\/","url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2613\/create-powershell-scripts-with-a-single-command\/","name":"Create PowerShell Scripts with a Single Command &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2613\/create-powershell-scripts-with-a-single-command\/#primaryimage"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2613\/create-powershell-scripts-with-a-single-command\/#primaryimage"},"thumbnailUrl":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/talkbubble-150x150.png","datePublished":"2012-12-05T18:04:27+00:00","dateModified":"2012-12-06T13:12:11+00:00","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2613\/create-powershell-scripts-with-a-single-command\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/2613\/create-powershell-scripts-with-a-single-command\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2613\/create-powershell-scripts-with-a-single-command\/#primaryimage","url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/talkbubble.png","contentUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/talkbubble.png","width":"198","height":"208"},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2613\/create-powershell-scripts-with-a-single-command\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"PowerShell","item":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},{"@type":"ListItem","position":2,"name":"Create PowerShell Scripts with a Single Command"}]},{"@type":"WebSite","@id":"https:\/\/jdhitsolutions.com\/blog\/#website","url":"https:\/\/jdhitsolutions.com\/blog\/","name":"The Lonely Administrator","description":"Practical Advice for the Automating IT Pro","publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jdhitsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9","name":"Jeffery Hicks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","caption":"Jeffery Hicks"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":933,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/933\/powershell-ise-new-function\/","url_meta":{"origin":2613,"position":0},"title":"PowerShell ISE New Function","author":"Jeffery Hicks","date":"September 16, 2010","format":false,"excerpt":"Yesterday I showed how to add a menu choice to the PowerShell ISE to insert the current date and time. Today I have something even better. At least it's something I've needed for awhile. I write a lot of advanced functions in PowerShell. I'm often cutting and pasting from previous\u2026","rel":"","context":"In &quot;PowerShell ISE&quot;","block_context":{"text":"PowerShell ISE","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-ise\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2010\/09\/ise-addons.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3990,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3990\/friday-fun-creating-powershell-scripts-with-powershell\/","url_meta":{"origin":2613,"position":1},"title":"Friday Fun: Creating PowerShell Scripts with PowerShell","author":"Jeffery Hicks","date":"September 5, 2014","format":false,"excerpt":"Sometimes PowerShell really does seem like magic. Especially when you can use it to handle complicated or tedious tasks, such as creating a PowerShell script. I know many an IT Pro who want to script but without having to actually write a script. Well, I'm not sure that is realistic,\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"012914_1704_CreatingCIM1.png","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2014\/01\/012914_1704_CreatingCIM1-150x150.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1330,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell-v2-0\/1330\/powershell-ise-convert-all-aliases\/","url_meta":{"origin":2613,"position":2},"title":"PowerShell ISE Convert All Aliases","author":"Jeffery Hicks","date":"April 8, 2011","format":false,"excerpt":"Yesterday I posted an article on how to convert a selected word to an alias or cmdlet. While I think there is still some value in this piecemeal approach. sometimes you want to make wholesale changes, such as when troubleshooting a script that someone else wrote that is full of\u2026","rel":"","context":"In &quot;PowerShell v2.0&quot;","block_context":{"text":"PowerShell v2.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-v2-0\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1340,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/1340\/convert-aliases-with-the-tokenizer\/","url_meta":{"origin":2613,"position":3},"title":"Convert Aliases with the Tokenizer","author":"Jeffery Hicks","date":"April 12, 2011","format":false,"excerpt":"Last week I posted a function you can use in the Windows PowerShell ISE to convert aliases to command definitions. My script relied on regular expressions to seek out and replace aliases. A number of people asked me why I didn't use the PowerShell tokenizer. My answer was that because\u2026","rel":"","context":"In &quot;PowerShell ISE&quot;","block_context":{"text":"PowerShell ISE","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-ise\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4305,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4305\/what-powershell-script-was-i-working-on\/","url_meta":{"origin":2613,"position":4},"title":"What PowerShell Script Was I Working On?","author":"Jeffery Hicks","date":"March 24, 2015","format":false,"excerpt":"Last week I shared a script for finding recently modified files in a given directory. In fact, it wouldn't be that difficult to find the last files I was working on and open them in the PowerShell ISE. Assuming my Get-RecentFile function is loaded it is a simple as this:\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4420,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/4420\/friday-fun-a-better-psedit\/","url_meta":{"origin":2613,"position":5},"title":"Friday Fun: A Better PSEdit","author":"Jeffery Hicks","date":"May 29, 2015","format":false,"excerpt":"In the PowerShell ISE, there is a built-in function called PSEdit. You can use this function to easily load a file in to the ISE directly from the ISE command prompt. Psedit c:\\scripts\\myscript.ps1 You can also load multiple files, but not as easily as you might like. I find myself\u2026","rel":"","context":"In &quot;Friday Fun&quot;","block_context":{"text":"Friday Fun","link":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2613","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=2613"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2613\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=2613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=2613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=2613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}