Skip to content

Convert-History2ps1

Converts history to PowerShell script

param([string]$path = "script-from-history.ps1")

try {
	$history = Get-History

	foreach ($item in $history) {
		Write-Output "`"? Step #$($item.Id): Executing $($item.CommandLine) ...`"" >> $path
		Write-Output "& $($item.CommandLine)" >> $path
		Write-Output "" >> $path
	}
	Write-Output "`"? PowerShell script finished (source: command history of $($env:USERNAME) on $($env:COMPUTERNAME)).`"" >> $path
	Write-Output "exit 0 # success" >> $path
	"? Converted your command history into PowerShell script: $path"
        exit 0 # success
} catch {
	"?? ERROR: $($Error[0]) (script line $($_.InvocationInfo.ScriptLineNumber))"
        exit 1
}

Specifies the file path of the new script ('script-from-history.ps1' by default)

An interactive directory of PowerShell scripts.