Windows Vista Forums » Discuss Microsoft ® Windows Vista ™ » Windows Vista Support » How to Add History to Windows PowerShell

Windows Vista Support A general support forum to discuss Windows Vista issues or problems not covered in other forums. Post here when your problem doesn’t fit in any other category.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-10-2006
hud's Avatar
hud hud is offline
Junior Member
 
Join Date: Jun 2006
Posts: 2
hud is on a distinguished road
How to Add History to Windows PowerShell
Ever wish you had history like in unix with .bash_history?

Here is some code that you can put in your profile file that defines a function BYE which saves off your sessions history into a History.CSV file in your home directory and then adds that to your history when you startup the next session. You get to control how much of the history you want (up to 32KB-1) but I just do 1KBs worth.
Code:
$MaximumHistoryCount = 1KB

if (!(Test-Path ~\PowerShell -PathType Container))
{   New-Item ~\PowerShell -ItemType Directory
}

function bye
{   Get-History -Count 1KB |Export-CSV ~\PowerShell\history.csv
    exit
}

if (Test-path ~\PowerShell\History.csv)
{   Import-CSV ~\PowerShell\History.csv |Add-History
}
I was going to put comments in the code but thought I would test out my assertion that VERBOSITY produces self-documenting scripts. I think it does but you can let me know one way or the other.

So add this to your startup and then do a session, type BYE instead of EXIT and then start a new session and do a Get-History. You'll see a bunch of commands already in your list.

Now lets have a little fun with History, do what I instructed above and get into your new session. Now do a Get-History and pipe it to Get-Member:

Code:
PS> Get-History |Get-Member -MemberType Property
   TypeName: Microsoft.PowerShell.Commands.HistoryInfo

Name               MemberType Definition
----               ---------- ----------
CommandLine        Property   System.String CommandLine {get;}
EndExecutionTime   Property   System.DateTime EndExecutionTime {get;}
ExecutionStatus    Property   System.Management.Automation.Runspaces.Pip...
Id                 Property   System.Int64 Id {get;}
StartExecutionTime Property   System.DateTime StartExecutionTime {get;}
This means that you can find out when something was executed (e.g. which session it happened in) using the following command:
Code:
PS> ghy |ft id,endexecutiontime,commandline -auto

 Id EndExecutionTime      CommandLine
 -- ----------------      -----------
612 6/29/2006 5:39:34 AM  gcm export-csv |fl *
613 6/30/2006 6:51:16 PM  ipconfig
614 6/30/2006 8:51:38 PM  cd C:\kits
http://blogs.msdn.com/powershell/arc...01/653194.aspx
__________________


Reply With Quote
Reply

Tags
powershell, history, add

Thread Tools
Display Modes

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
After Vista - Windows Horizon codename 7 Graham General Discussion 0 01-31-2008 01:28 AM
1.Speed up 2.virus removal virusremoval Windows Vista Support 1 09-27-2007 01:30 PM
Top 10 Windows Vista Speed Tweaks bios Windows Vista Support 5 09-21-2007 07:33 AM
Installation Problems of Windows Vista Beta 2 rayferns Windows Vista Installation 4 10-07-2006 08:45 PM
Microsoft Windows Media Player Plugin Buffer Overflow Vulnerability Advisory josh_rain General Discussion 1 02-14-2006 08:56 PM




SEO by vBSEO 3.2.0 ©2008, Crawlability, Inc.