Skip to content

Instantly share code, notes, and snippets.

@steviecoaster
Last active November 8, 2017 18:50
Show Gist options
  • Save steviecoaster/9953cefed1ddec8e3ac792d1f52df13c to your computer and use it in GitHub Desktop.
Save steviecoaster/9953cefed1ddec8e3ac792d1f52df13c to your computer and use it in GitHub Desktop.
Automatically create a transcript of your Powershell Session. Keeps 7 days worth of history by default
<#
.SYNOPSIS
Automatically create transcript of your Powershell session.
.DESCRIPTION
Add this function to your Powershell Profile and call it with Invoke-SessionLog -Logpath \some\path to automatically keep
session history each time you open up powershell.
.EXAMPLE
Invoke-SessionLog -LogPath C:\PSLogs -DaysToKeep 7
#>
Function Invoke-SessionLog {
Param(
[cmdletBinding()]
[Parameter(Mandatory, Position = 0)]
[String]$LogPath,
[Parameter(Mandatory,Position = 1)]
[String]$DaysToKeep
)
$Oldlogs = Get-ChildItem -Path $LogPath -Recurse -Force
foreach ($log in $Oldlogs) {
If ($log.CreationTime -lt (Get-Date).AddDays(-$DaysToKeep)) {
Remove-Item $log -Force
}
}
Start-Transcript -OutputDirectory $LogPath
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment