-
Notifications
You must be signed in to change notification settings - Fork 50
/
Export-History.ps1
46 lines (39 loc) · 1.49 KB
/
Export-History.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function Export-history {
<#
.NOTES
===========================================================================
Created by: Brian Graf
Date: 4/13/2017
Organization: VMware
Blog: http://www.BrianJGraf.com
Twitter: @vBrianGraf
Github: https://github.com/vtagion
===========================================================================
.SYNOPSIS
Cmdlet to export all commands run in a PowerShell session to a text file
.DESCRIPTION
If you are like me you will likely have multiple PowerShell sessions open and literally hundreds of lines of commands run.
There are times you need to close these, or reorganize, etc and the one thing you don't want is to lose all the things
you've run without saving them. Get-History is a great tool, but why not export that history
.EXAMPLE
PS C:\> Export-History -File c:\temp\PowerCLISessionHistory.txt
#>
param (
$File,
[string]$Description
)
begin {
if (!(test-path $file)) {New-Item $file -type file}
}
Process {
if ($Description) {
Write-Output "$Description" | out-file -Append $File
} Else { Write-Output "------------------Start of file-----------------" | out-file -Append $File
}
foreach ($line in (get-history)) { $line.Commandline | out-file -Append $File }
Write-Output "------------------End of file-----------------" | out-file -Append $File
}
End {
Write-Host "Export Successful!" -ForegroundColor Green
}
}