Main | Contact | Blog | Documentation

PowerShell Framework

The project dedicated to empowering your PowerShell scripting.

Getting Started with Logging

In order to create logs, all you need to do is update existing scripts and replace all instances of …

Write-Verbose

… with …

Write-PSFMessage

This is all that it takes, your scripts are now creating logfiles.

Note: Replacing custom functions such as the often used Write-Log works just the same - just send it the message and Write-PSFMessage will handle the rest.

Example

# Setting up the logging
$paramSetPSFLoggingProvider = @{
    Name         = 'logfile'
    InstanceName = '<taskname>'
    FilePath     = 'C:\Logs\TaskName-%Date%.csv'
    Enabled      = $true
    Wait         = $true
}
Set-PSFLoggingProvider @paramSetPSFLoggingProvider

# Start Logging
Write-PSFMessage "Starting Script"

# Add script code here, using Write-PSFMessage wherever you want to log
# ...

Write-PSFMessage "Script Completed"

# Wait for logging to complete
Wait-PSFMessage

Additional information

Further reading