Creating New Scripts in ISE

by Jul 12, 2012

Often, you first play around with PowerShell commands interactively, and then once those commands do what you want, you can copy them to your script editor and turn them into scripts and functions.

Here is a little helper function that takes all your interactive commands entered in the PowerShell ISE editor and opens them all as a new script inside ISE.

function New-Script 
{
    $text = Get-History | Select-Object -ExpandProperty CommandLine | Out-String
    $file = $psise.CurrentPowerShellTab.Files.Add()
    $file.Editor.text = $text
}

Note that this function runs only inside PowerShell ISE. Note also that you can "extend" the command line buffer so ISE remembers more commands:

$MaximumHistoryCount = 20000

Twitter This Tip! ReTweet this Tip!