Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Wednesday, October 17, 2012

Automatically create a log of your PowerShell sessions

In my October 3rd article, I wrote about using the PowerShell transcript feature to create a log file of everything you do.  This is a good idea, but it is a manual task.  Here is how you make it automatic.  We are going to use your PowerShell profile.  If you have not yet created a profile, take a look at my blog entry on September 24, 2012.

First, create a location to store your transcripts.  This file will contain a lot so it is best to get it organized now. My file is in My Documents in the PowerShell\Transcript folder.  You put yours where it makes sense to you.

Now, open your profile.  In PowerShell, type PowerShell_ise $Profile.

Enter this set of code in your profile.

# -- Automate PowerShell Transcription --
Create a filename based on a time stamp.
$Filename = ((Get-date).Month).ToString()+"-"+`
# Set the storage path.
$Path = "C:\Users\Jason\Documents\PowerShell\Transcript"
# Turn on PowerShell transcripting.
Start-Transcript -Path "$Path\$Filename"
# ---------------------------------------

This will start the transaction functionality every time you start the PowerShell environment, and put it in the folder specified in the –Path parameter.  Now save your profile.  Close the shell and reopen it.

Your session will start similar to this:

Windows PowerShell
Copyright (C) 2012 Microsoft Corporation. All rights reserved.

Transcript started, output file is C:\Users\Jason\Documents\PowerShell\Transcript\8-12-2012-19-32-48.txt
PS C:\Users\Jason>

Take a look in your designated log location, you now have transcripts starting automatically for each session.


No comments: