PoshCode Logo PowerShell Code Repository

New-Script 1.1 by Joel Bennett 3 years ago (modification of post by Joel Bennett view diff)
View followups from Joel Bennett | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/168"></script>download | new post

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

Fixes a bug in the first one which made you unable to specify multiple IDs as intended.

  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 ScriptName -id 10,11,12,14
  8. ##        creates a script from the specified commands in history
  9. ##    New-Script Clipboard 2
  10. ##        sends the most recent two commands to the clipboard
  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. ##################################################################################################
  22.  
  23. #function New-Script {
  24. param(
  25.    [string]$script=(Read-Host "A name for your script"),
  26.    [int]$count=1,
  27.    [int[]]$id=@((Get-History -count 1| Select Id).Id)
  28. )
  29.  
  30. $commands = &{if($id.Count -gt 1){ Get-History -id $id } else { Get-History -count $count }} | &{process{ $_.CommandLine }}
  31.  
  32. if($script -eq "clipboard") {
  33.    if( @(Get-PSSnapin -Name "pscx").Count ) {
  34.       $commands | out-clipboard
  35.    }elseif(@(gcm clip.exe).Count) {
  36.       $commands | clip
  37.    }
  38. } else {
  39.    # default to putting it in my "Windows PowerShell\scripts" folder which I have in my path...
  40.    $folder = Split-Path $script
  41.    if(!$folder) {
  42.       $folder = Join-Path (Split-Path $Profile) "Scripts"
  43.    }
  44.    if(!(Test-Path $folder)) {
  45.       Throw (new-object System.IO.DirectoryNotFoundException "Cannot find path '$folder' because it does not exist.")
  46.    }
  47.    # add the ps1 extension if it's not already there ...
  48.    $file = Join-Path $folder (Split-Path $script -leaf)
  49.    if(!(([IO.FileInfo]$file).Extension)) {
  50.       $file = "$file.ps1"
  51.    }
  52.    $commands | set-content $file
  53. }
  54. #}

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