Navigate / search

PowerShell – General Logging Script

Working in enterprise environments always requires a confidential log for scripting actions implemented in productive stages and even for testing it could be a great advantage. PowerShell offers a great possibility to log every action executed by a script. The CMDlets  Start-Transcript  & Stop-Transcript will help you to achieve a successful logging script.

This will be just an example how you can easyly create a logging script. There are thousands of different ways to solve this. The Microsoft documentation can be found in the following link

The following script will write a general log file to  C:\temp\logging.log containing all relevant basis information about the script execution.


As you can see with only two lines of code you can achieve a good basis for logging.

plus Adding Timestamp

Let us extend the script that in the end we are able to produce a more detailed logging suitable for enterprise. When executing the script you know when the script was started and when its ended but in some case you need to know when every single code line was executed by your script. Therefore lets implement the function  Write-Log($string)


The logging is indeed easier to read also by technicians that are not well versed in programming or scripting. But when something went wrong the log file doesn’t display were we can find the executed script file to reproduce the error.

plus Adding Script Location

In large environments you never know if the file was executed from a fileshare or from a local drive. Lets fix that by implemnting the function  function Get-ScriptName


As you can see I’ve executed the file from the desktop. Don’t do that in productive environments. For testing only please 😉


Leave a comment


email* (not published)


This site uses Akismet to reduce spam. Learn how your comment data is processed.