Final update on MSH/Powershell History command
Thanks again to Jeffrey Snover, I've finally gotten the history to be preserved between MSH sessions. It took a little tweaking in order to get it right (for me), but here are the steps I followed.
- Create a profile.msh file. Within the Command Shell, if you run $profile, it will tell you where your profile is. At first, I was struggling with this part because I haven't been doing a lot of work with Powershell. A few web searches yielded the information I needed, which indicated that within my 'My Documents' folder, I needed to create a directory named 'msh', and then create a file within that msh directory called profile.msh. Of note here is that (at least on my system) you can't directly edit .msh files. So each time I wanted to edit the contents, I had to first rename it to a .txt extension. No biggie.
- Add the code to the profile.msh file. I had to make some changes to the code that Jeffrey posted. I'm not sure if this was due to me using the Exchange Management Shell instead of Windows Powershell (though they really are the same thing), but I'll first post the initial code that Jeffrey posted, and then show you what changes I had to make.
$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
}
OK – the first problem I ran into was that with the above code, each time I launched the Exchange Management Shell, it would throw an error stating that 1KB was an invalid integer. Once I finally got it through my thick head that I indeed needed to supply an actual integer, that was easy enough to fix :-)
The second problem I ran into was the first if statement. It was complaining that it couldn't find a parameter that matches the parameter '-PathType'. I'm not sure what that was all about (it appears to be having a problem creating the 'Powershell' directory), but I wanted to get this working, so I removed those lines. I ended up with the following code that works. All you have to do is type 'Bye' instead of Exit (this also means you can't click the 'X' to close the Shell window), and your history will be preserved. If you'd like to use this, feel free. If you'd rather, you can also change the export function to export-clixml, which will export it to an XML file instead of a CSV file. You would then also need to change the import function to import-clixml. The below code will save the history.csv to the root of your user profile.
$MaximumHistoryCount = 10000
function bye
{ Get-History -Count $MaximumHistoryCount Export-CSV ~\history.csv
exit
}
if (Test-path ~\History.csv)
{ Import-CSV ~\History.csv Add-History
}
update: had to edit this post as the code didn't come across right from Word. It should be good now.
PSMDFAQ: How do you save history across powershell sessions?
<< Home