Home > Exchange 2010, Powershell > Logging All Powershell Commands and Output

Logging All Powershell Commands and Output

There have been a number of occasions where I have accidentally closed out of powershell, and had to retype a script that took some time to create, or wanted to go back to see what commands I had already run. Usually the up Arrow Key is sufficient for this, however what about commands that you ran a few days ago?

This post will walk you through setting up a powershell profile to automatically log all activity within powershell.

1. Create the Folder Path for the script output file. Mine is C:\PowershellLog

2. We need to allow the custom profile to be able to execute. This will allow Powershell to execute .ps1 scripts written by you, but will still prompt for unsigned scripts from the internet. To allow powershell to execute custom ps1 scripts, open Powershell and run the following command.

Set-ExecutionPolicy RemoteSigned

3. Next create a Powershell profile that will be loaded every time powershell is opened. Type the exact command below.

New-item -type file -path $profile -force

4. The profile can now be edited to execute custom commands at startup. This can include loading custom aliases, setting the powershell window colors, heading etc.. Today I will just be using it to setup a transcript for Powershell.  To edit the profile in Notepad, type the following in Powershell.

notepad $profile

5. This will open your profile in notepad. We can now set the default location, add a time stamp as well as a transcript file to the profile. Now every time powershell is opened, it will log the date and time, as well as append all commands and outputs to a text file. This is what the entirety of my profile looks like.

Set-Location C:\
start-transcript C:\PowershellLog\PSTranscript.txt -append -noclobber
$a = Get-Date
“Date: ” + $a.ToShortDateString()
“Time: ” + $a.ToShortTimeString()

6. Save the notepad file. Now if I browse to C:\PowershellLog\PSTranscript.txt, I will see the following logged for every new powershell session I open, as well as my complete history of everything that has ever been run in powershell, as well as the associated outputs that were displayed in powershell.

Transcript started, output file is C:\PSTranscirpts.txt
Date: 8/5/2011
Time: 1:42 PM
PS C:\>

This will also work for Exchange Management Shell, or any other pre-baked Powershell application. Pretty cool huh? Note that this only applies to the current user. I may address doing this for multiple users at a later date. Let me know if that is something you would be interested in seeing.

About these ads
Categories: Exchange 2010, Powershell
  1. Pawel
    December 13, 2011 at 11:18 am | #1

    Very useful. Thank you

  2. JD
    September 6, 2012 at 7:43 am | #2

    thanks for this.

  3. December 4, 2012 at 1:49 pm | #3

    Very helpful, thank you!

  4. Raymond Anders
    July 31, 2013 at 6:58 am | #4

    I concur, very helpful. Thank you.

  5. sathyan
    August 1, 2013 at 8:29 am | #5

    How to make this work for multiple users who log on to same server ? Please help

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: