PoshTip #47 – Save a Command into PowerShell History

Everybody knows that when you type a PowerShell command, this command will be automatically saved into history. Then, you can get the history with the following command:

PS > Get-History
or
PS > h
or 
PS > History

But sometimes you may want to save a command into history without executing it for later execution or just for information.

You also can save a comment into history. Of course, you can do something like that:

PS > Start history
Start : This command cannot be run due to the error: The system cannot find the file specified.
At line:1 char:1
+ Start history
+ ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

PS > Get-Date

Sunday, April 30, 2017 13:54:59

PS > gci

    Directory: C:\Users\Nicolas

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-r---       04/12/2017     07:03                Contacts
d-r---       04/12/2017     07:03                Desktop
d-r---       04/29/2017     19:10                Documents
d-r---       04/29/2017     19:36                Downloads
d-r---       04/12/2017     07:03                Favorites
d-r---       04/12/2017     07:03                Links
d-r---       04/12/2017     07:03                Music
d-r---       12/18/2016     12:06                OneDrive
d-r---       04/12/2017     07:03                Pictures
d-r---       04/12/2017     07:03                Saved Games
d-r---       04/12/2017     07:03                Searches
d-r---       04/12/2017     07:03                Videos


PS > End history
At line:1 char:4
+ End history
+    ~
Missing statement block after 'end'.
At line:1 char:5
+ End history
+     ~~~~~~~
Unexpected token 'history' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingNamedStatementBlock

Now, I can check the history:

PS > h

  Id CommandLine
  -- -----------
   1 Start history
   2 Get-Date
   3 gci
   4 End history

It works but you will get a lot of error messages into the PowerShell console. So the best practice is to use the # character before your comment or command:

PS > # Start history
PS > Get-Date

Sunday, April 30, 2017 13:32:06

PS > gci

    Directory: C:\Users\Nicolas

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-r---       04/12/2017     07:03                Contacts
d-r---       04/12/2017     07:03                Desktop
d-r---       04/29/2017     19:10                Documents
d-r---       04/29/2017     19:36                Downloads
d-r---       04/12/2017     07:03                Favorites
d-r---       04/12/2017     07:03                Links
d-r---       04/12/2017     07:03                Music
d-r---       12/18/2016     12:06                OneDrive
d-r---       04/12/2017     07:03                Pictures
d-r---       04/12/2017     07:03                Saved Games
d-r---       04/12/2017     07:03                Searches
d-r---       04/12/2017     07:03                Videos


PS > # End history
PS > h

  Id CommandLine
  -- -----------
   1 # Start history
   2 Get-Date
   3 gci
   4 # End history

It can be useful or just a gadget! It’s up to you!

 

Thanks for reading! You can follow me on Twitter @PrigentNico

About Nicolas 276 Articles
I work as an IT Production Manager, based in Paris (France) with a primary focus on Microsoft technologies. I have 10 years experience in administering Windows Servers. . I am a Microsoft MVP for Cloud & Datacenter Management. I also received the PowerShell Hero 2016 award by PowerShell.0rg. And finally, I am "MCSE: Cloud Platform and Infrastructure", "MCSA: Windows Servers", "Administering & Deploying SCCM", and CheckPoint CCSA certified.