How to keep track of work in Powershell

What to keep as a work record

When you do some work, you may be asked for a work record. Sometimes referred to as evidence or trails, it's important to keep track of what you did and what happened. When working with Powershell, keep the following two records:

Record command output

A record of what was output when the command was executed. There are several ways to record output with Powerehll.

In addition to this, there is a method of selecting the content output on the screen and saving it by copying, but the explanation is omitted.

Command history

History of executed commands. How to get command execution history in Powershell

-Copy history file ・ Get-History

How to record the output of a command

Save command output to text with redirect

Use> (redirect) to output the executed content as it is to text. Orthodox method.

[command] > [text file].txt

Specify the text file to be output by adding> after the command.

PS > echo "Sea urchin" > test.txt
PS > Get-Content test.txt
Sea urchin

The redirected text file will be overwritten. If you want to add it, redirect with >>.

PS > echo "Sea urchin" >> test.txt

When using a redirect, the command execution result is not displayed on the screen. If you want to display it on the screen, use Tee-Object.

PS > echo "Sea urchin" | Tee-Object test.txt
Sea urchin
PS > Get-Content test.txt
Sea urchin

Record the contents output on the screen to a file

Execute the following command when starting and ending the recording of text.

Start-Transcript → Start recording Stop-Transcript → End of recording

** Start recording **

> Start-Transcript
The transcript has started. Output file: [Current path]\PowerShell_transcript.HORUS.Wm+ZpDyv.[Date and time].txt

Recording starts after the command is executed.

** End of recording **

> Stop-Transcript
The transcript has been stopped. Output file: [Current path]\Documents\PowerShell_transcript.HORUS.Wm+ZpDyv.[Date and time].txt

Recording is stopped after the command is executed. Various information is recorded in the header of the saved text file. The header of the output file is as follows.

** Output content **

**********************
Start Windows PowerShell transcript
Start time: 20200111180312
username: [User]
RunAs user: [User]
Configuration name: 
computer: [hostname](Microsoft Windows NT 10.0.18362.)
Host application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Process ID: 13508
PSVersion: 5.1.18362.145
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.18362.145
BuildVersion: 10.0.18362.145
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************

Information such as the start time, end time, and host name of recording is automatically entered.

Save the command execution result to a CSV file

Pipe the output of the command to Export-Csv. The output is saved in the file attached to the option.

<command> | Export-Csv <file name>.csv

Since the output output to the screen is formatted in CSV, the data that is not displayed is not saved. The output CSV can be read as it is with Excel or the like.

Save the command execution result in an xml file

Pipe the output of the command to Export-Clixml. The output is saved in the file attached to the option.

<command> | Export-Clixml <file name>.xml

Import the xml file output by the following command.

Import-Clixml <file name>.xml

After importing, you will see the exported output. Put the output in a variable or process it through a pipe to utilize it.

Save command execution history

File in which execution history is recorded

When you execute a command with Powershell, the executed command is output to a text file as a history and saved. You can check the location of the saved file by executing the following command.

(Get-PSReadLineOption).HistorySavePath

Also, it seems that you can change the output location with the following command.

 Set-PSReadLineOption -HistorySavePath [File path to change]

However, once you close the prompt, it will revert to the default location.

Display history with command

You can display the command execution history with the ** Get-history ** command. 。

PS C:\Users\Main> Get-History

  Id CommandLine
  -- -----------
   1 Get-History
   2 Get-History

If you have properties, you can also display command execution history, execution time, and end time data.

item Contents
ExecutionStatus Command execution result
StartExecutionTime Command execution start time
EndExecutionTime Command execution end time

When all properties are displayed with Select-Object *, this information is displayed.

PS C:\Users\Main> Get-History | Select-Object *


Id                 : 1
CommandLine        : Get-History | Select-Object *
ExecutionStatus    : Completed
StartExecutionTime : 2020/01/16 19:54:41
EndExecutionTime   : 2020/01/16 19:54:41

Id                 : 2
CommandLine        : Get-History | Select-Object *
ExecutionStatus    : Completed
StartExecutionTime : 2020/01/16 19:54:43
EndExecutionTime   : 2020/01/16 19:54:43

With Powershell, you can combine command execution history and output history to reliably record when and what kind of data was acquired.

Recommended Posts

How to keep track of work in Powershell
How to work with BigQuery in Python
Summary of how to import files in Python 3
Summary of how to use MNIST in Python
How to get the number of digits in Python
How to display multiple images of galaxies in tiles
[Work efficiency] How to change file names in Python
How to develop in Python
How to keep conda off
How to find the optimal number of clusters in k-means
How to get rid of server custom emoji in message.content
How to develop in a virtual environment of Python [Memo]
Comparison of how to use higher-order functions in Python 2 and 3
How to get a list of built-in exceptions in python
Summary of how to write .proto files used in gRPC
How to get an overview of your data in Pandas
How to get a quadratic array of squares in a spiral!
Parallel processing of Python joblib does not work in uWSGI environment. How to process in parallel on uWSGI?
Ventilation is important. What I did to keep track of the C02 concentration in the room
[Python] How to do PCA in Python
Summary of how to use pandas.DataFrame.loc
How to use classes in Theano
How to write soberly in pandas
How to collect images in Python
How to update Spyder in Anaconda
How to use SQLite in Python
How to use virtualenv with PowerShell
Summary of how to use pyenv-virtualenv
How to convert 0.5 to 1056964608 in one shot
How to reflect CSS in Django
How to kill processes in bulk
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to run TensorFlow 1.0 code in 2.0
Summary of how to use csvkit
How to handle Japanese in Python
How to log in to Docker + NGINX
How to call PyTorch in Julia
How to display a specified column of files in Linux (awk)
How to handle multiple versions of CUDA in the same environment
How to determine the existence of a selenium element in Python
How to implement Java code in the background of RedHat (LinuxONE)
How to know the internal structure of an object in Python
How to change the color of just the button pressed in Tkinter
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
[TensorFlow 2] How to check the contents of Tensor in graph mode
How to get the vertex coordinates of a feature in ArcPy
How to use machine learning for work? 01_ Understand the purpose of machine learning
How to create a large amount of test data in MySQL? ??
How to use calculated columns in CASTable
How to resolve "No kernel of grammar Python found" error in Atom
[Python] Summary of how to use pandas
[Introduction to Python] How to use class in Python?
How to suppress display error in matplotlib
Introduction to Machine Learning: How Models Work
How to speed up instantiation of BeautifulSoup
How to access environment variables in Python
How to change multiple columns of csv in Pandas (Unixtime-> Japan Time)