Writing Logs in Powershell Script in SharePoint

Hello All,

In our project we need to write lots of Powershell scripts. So just finding the ways to maintain logs in Powershell scripts.

There is a Powershell command “Start-Transcript” which allows you to write into a log file.

Syntax:

Start-Transcript <Log File Path>

<Log File Path>: Log file path in which logs are maintained.

After this command we can use either write-output or write-host. Everything used with these commands is get logged into log file.

Whenever if we want to stop the maintain logs there is command “Stop-Transcript”.

Example:

$logFilePath = “c:\logfile.log” #Log File Path contains folder name and file name.

    Start-Transcript $ logFilePath
    write-output "Logging Started”
    Stop-Transcript

In this way logs are maintained for powershell script.

Thanks!
Br,
Prasham