PoshCode Logo PowerShell Code Repository

New-Script 2 (modification of post by Joel Bennett view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/171"></script>download | new post

Create a new script from a series of commands in your history… or copy them to the clipboard.

Use the current folder if you don’t have a WindowsPowershell\Scripts folder, and be more careful about overwriting existing scripts

  1. ## New-Script function
  2. ## Creates a new script from the most recent commands in history
  3. ##################################################################################################
  4. ## Example Usage:
  5. ##    New-Script ScriptName 4
  6. ##        creates a script from the most recent four commands
  7. ##    New-Script Clipboard -id 10,11,12,14
  8. ##        sends the the specified commands from the history to the clipboard
  9. ##    Notepad (New-Script ScriptName 20)
  10. ##        sends the most recent twenty commands to the script, and then opens the script in notepad
  11. ##################################################################################################
  12. ## As a tip, I use a prompt function something like this to get the ID into the prompt:
  13. ##
  14. ## function prompt {
  15. ##   return "`[{0}]: " -f ((get-history -count 1).Id + 1)
  16. ## }
  17. ##################################################################################################
  18. ## Revision History
  19. ## 1.0 - initial release
  20. ## 1.1 - fix bug with specifying multiple IDs
  21. ## 2.0 - use the current folder as the default instead of throwing an exception
  22. ##     - prompt to overwrite if not -Force
  23. ##################################################################################################
  24.  
  25. #function New-Script {
  26. param(
  27.    [string]$script=(Read-Host "A name for your script"),
  28.    [int]$count=1,
  29.    [int[]]$id=@((Get-History -count 1| Select Id).Id),
  30.    [switch]$Force
  31. )
  32.  
  33. # if there's only one id, then the count counts, otherwise we just use the ids
  34. if($id.Count -eq 1) { 1..($count-1)|%{ $id += $id[-1]-1 } }
  35. # Get the CommandLines from the history items...
  36. $commands = Get-History -id $id | &{process{ $_.CommandLine }}
  37.  
  38. if($script -eq "clipboard") {
  39.    if( @(Get-PSSnapin -Name "pscx").Count ) {
  40.       $commands | out-clipboard
  41.    } elseif(@(gcm clip.exe).Count) {
  42.       $commands | clip
  43.    }
  44. } else {
  45.    $folder = Split-Path $script
  46.    if(!$folder) {
  47.       # default to putting it in my "Windows PowerShell\scripts" folder which I have in my path...
  48.       $folder = Join-Path (Split-Path $Profile) "Scripts"
  49.    }
  50.    if(!(Test-Path $folder)) {
  51.       # if that fails, put it in the current path (on the file system)
  52.       $folder = Get-Location -PSProvider "FileSystem"
  53.    }
  54.    # add the ps1 extension if it's not already there ...
  55.    $file = Join-Path $folder (Split-Path $script -leaf)
  56.    if(!(([IO.FileInfo]$file).Extension)) {
  57.       $file = "$file.ps1"
  58.    }
  59.    # write an error message if the file already exists, unless -Force
  60.    if((Test-Path $file) -and (!$Force)) {
  61.       Write-Error "The file already exists, do you want to overwrite?"
  62.    }
  63.    # and confirm before setting the content if the file already exists, unless -Force
  64.    $commands | set-content $file -Confirm:((Test-Path $file) -and (!$Force))
  65.    Get-Item $file
  66. }
  67. #}

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me