PoshCode Archive  Artifact [82ca44dad7]

Artifact 82ca44dad7b540bb04df0533b23644624e611e5703333b557bd5ed9f6cffc44f:

  • File New-Script-2.ps1 — part of check-in [48c18aa5b8] at 2018-06-10 13:00:19 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: 3370)

# encoding: ascii
# api: powershell
# title: New-Script 2
# 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: 171
# x-archived: 2016-03-04T23:52:22
# x-published: 2009-04-11T19:57:00
#
# Use the current folder if you don’t have a WindowsPowershell\Scripts folder, and be more careful about overwriting existing scripts
#
## 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 Clipboard -id 10,11,12,14
##        sends the the specified commands from the history to the clipboard
##    Notepad (New-Script ScriptName 20)
##        sends the most recent twenty commands to the script, and then opens the script in notepad
##################################################################################################
## 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
## 2.0 - use the current folder as the default instead of throwing an exception
##     - prompt to overwrite if not -Force
##################################################################################################

#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),
   [switch]$Force
)

# if there's only one id, then the count counts, otherwise we just use the ids
if($id.Count -eq 1) { 1..($count-1)|%{ $id += $id[-1]-1 } }
# Get the CommandLines from the history items...
$commands = Get-History -id $id | &{process{ $_.CommandLine }}

if($script -eq "clipboard") {
   if( @(Get-PSSnapin -Name "pscx").Count ) {
      $commands | out-clipboard
   } elseif(@(gcm clip.exe).Count) {
      $commands | clip
   }
} else {
   $folder = Split-Path $script
   if(!$folder) {
      # default to putting it in my "Windows PowerShell\scripts" folder which I have in my path...
      $folder = Join-Path (Split-Path $Profile) "Scripts"
   }
   if(!(Test-Path $folder)) { 
      # if that fails, put it in the current path (on the file system)
      $folder = Get-Location -PSProvider "FileSystem"
   }
   # 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"
   }
   # write an error message if the file already exists, unless -Force
   if((Test-Path $file) -and (!$Force)) {
      Write-Error "The file already exists, do you want to overwrite?"
   }
   # and confirm before setting the content if the file already exists, unless -Force
   $commands | set-content $file -Confirm:((Test-Path $file) -and (!$Force))
   Get-Item $file
}
#}