PowerShell/docs/convert-history2ps1.md
2025-05-12 22:04:02 +02:00

2.5 KiB

The convert-history2ps1.ps1 Script

This PowerShell script converts your command history into a PowerShell script. It saves all your commands into a script for automation (executed by e.g Jenkins or AutoHotkey).

Parameters

/Repos/PowerShell/scripts/convert-history2ps1.ps1 [[-path] <String>] [<CommonParameters>]

-path <String>
    Specifies the file path of the new script ('script-from-history.ps1' by default)
    
    Required?                    false
    Position?                    1
    Default value                script-from-history.ps1
    Accept pipeline input?       false
    Aliases                      
    Accept wildcard characters?  false

[<CommonParameters>]
    This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, 
    WarningVariable, OutBuffer, PipelineVariable, and OutVariable.

Example

PS> ./convert-history2ps1.ps1
 Converted your command history into the PowerShell script: script-from-history.ps1

Notes

Author: Markus Fleschutz | License: CC0

https://github.com/fleschutz/PowerShell

Script Content

<#
.SYNOPSIS
        Converts history to PowerShell script
.DESCRIPTION
        This PowerShell script converts your command history into a PowerShell script.
	It saves all your commands into a script for automation (executed by e.g Jenkins or AutoHotkey).
.PARAMETER path
        Specifies the file path of the new script ('script-from-history.ps1' by default)
.EXAMPLE
        PS> ./convert-history2ps1.ps1
	✅ Converted your command history into the PowerShell script: script-from-history.ps1
.LINK
        https://github.com/fleschutz/PowerShell
.NOTES
        Author: Markus Fleschutz | License: CC0
#>

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 in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
        exit 1
}

(page generated by convert-ps2md.ps1 as of 05/12/2025 22:02:53)