PoshCode Archive  Artifact [b55cc3814c]

Artifact b55cc3814c31a92e2939ef87a582a88dc823e28905eef13c8876f1337e975eb6:

  • File New-Script.ps1 — part of check-in [d53df340bf] at 2018-06-10 12:59:57 on branch trunk — Create a new script from a series of commands in your history… or copy them to the clipboard. (user: Joel Bennett size: 2659)

# encoding: ascii
# api: powershell
# title: New-Script
# description: Create a new script from a series of commands in your history… or copy them to the clipboard.
# version: 1.1
# type: script
# author: Joel Bennett
# license: CC0
# function: New-Script
# x-poshcode-id: 168
# x-derived-from-id: 171
# x-archived: 2016-03-15T16:28:17
# x-published: 2009-04-09T12:51:00
#
# Fixes a bug in the first one which made you unable to specify multiple IDs as intended.
#
## New-Script function
## Creates a new script from the most recent commands in history
##################################################################################################
## Example Usage:
##    New-Script ScriptName 4
##        creates a script from the most recent four commands 
##    New-Script ScriptName -id 10,11,12,14
##        creates a script from the specified commands in history
##    New-Script Clipboard 2
##        sends the most recent two commands to the clipboard
##################################################################################################
## As a tip, I use a prompt function something like this to get the ID into the prompt:
##
## function prompt {
##   return "`[{0}]: " -f ((get-history -count 1).Id + 1)
## }
##################################################################################################
## Revision History
## 1.0 - initial release
## 1.1 - fix bug with specifying multiple IDs
##################################################################################################

#function New-Script {
param( 
   [string]$script=(Read-Host "A name for your script"),
   [int]$count=1, 
   [int[]]$id=@((Get-History -count 1| Select Id).Id)
)

$commands = &{if($id.Count -gt 1){ Get-History -id $id } else { Get-History -count $count }} | &{process{ $_.CommandLine }}

if($script -eq "clipboard") {
   if( @(Get-PSSnapin -Name "pscx").Count ) {
      $commands | out-clipboard
   }elseif(@(gcm clip.exe).Count) {
      $commands | clip
   }
} else {
   # default to putting it in my "Windows PowerShell\scripts" folder which I have in my path...
   $folder = Split-Path $script
   if(!$folder) {
      $folder = Join-Path (Split-Path $Profile) "Scripts"
   }
   if(!(Test-Path $folder)) { 
      Throw (new-object System.IO.DirectoryNotFoundException "Cannot find path '$folder' because it does not exist.")
   }
   # add the ps1 extension if it's not already there ...
   $file = Join-Path $folder (Split-Path $script -leaf)
   if(!(([IO.FileInfo]$file).Extension)) { 
      $file = "$file.ps1"
   }
   $commands | set-content $file
}
#}