Skip to content

Instantly share code, notes, and snippets.

@dancing-groot
Last active April 27, 2026 17:04
Show Gist options
  • Select an option

  • Save dancing-groot/6af20a68e67306762905665db339d30d to your computer and use it in GitHub Desktop.

Select an option

Save dancing-groot/6af20a68e67306762905665db339d30d to your computer and use it in GitHub Desktop.
Function for writing to a log file in plain format, with the option to write the information to the host as well
[CmdletBinding(SupportsShouldProcess)]
param()
#region FUNCTIONS
function New-SimpleLog
{
<#
.SYNOPSIS
Write information to a log file and optionally to the console
.LINK
https://gist.github.com/dancing-groot/6af20a68e67306762905665db339d30d
.NOTES
Version: 2026.03.27
Author: @dancing-groot
#>
[cmdletbinding(SupportsShouldProcess)]
param (
[string]$LogFile = "$($ENV:TEMP)\$((Split-Path -Leaf $script:MyInvocation.MyCommand.Path).Trim('.ps1'))-$(Get-Date -UFormat '%Y.%m.%d')-$(Get-Date -UFormat '%H%M').log",
[ValidateSet('Log', 'Console', 'All')]
[string]$WriteTo = "Log"
)
if ($script:PSBoundParameters['Verbose']) { $script:VerbosePreference = 'Continue' }
if ($script:PSBoundParameters['Debug']) { $script:DebugPreference = 'Continue' }
if ($WriteTo -in @('Log', 'All'))
{
# This can break within scheduled tasks
$LogFile = $LogFile.Replace("\\", "\")
if ((Test-Path -LiteralPath (Split-Path $LogFile -Parent)) -eq $false) { New-Item -Path (Split-Path $LogFile -Parent) -Type Directory | Out-Null }
} else {
$LogFile = $null
}
# If -WhatIf is called for this function, pass it on to Write-SimpleLog
# E.g. you may be running the script with -WhatIf, but if you still need to write to the console and/or log use
# New-SimpleLog [parameters] -WhatIf:$false
return @{Path = $LogFile; WriteTo = $WriteTo; WhatIf = (-not $PSCmdlet.ShouldProcess("Write Log")) }
} # New-SimpleLog
function Write-SimpleLog
{
<#
.SYNOPSIS
Write information to a log file and optionally to the console
.LINK
https://gist.github.com/dancing-groot/6af20a68e67306762905665db339d30d
.NOTES
Version: 2026.04.27
Author: @dancing-groot
#>
[cmdletbinding(SupportsShouldProcess)]
param (
[string]$Path,
#[parameter(Mandatory = $true)]
[string]$Message,
[ValidateSet('Warning', 'Error', 'Verbose', 'Debug', 'Info')]
[string]$Type = "Info",
[ValidateSet('Log', 'Console', 'All')]
[string]$WriteTo = "Log"
)
try
{
$content = "[$(Get-Date -UFormat '%Y.%m.%d %T')] $($Type.ToUpper())".PadRight(30) + " $Message"
# Write the line to the log file
switch ($true)
{
($WriteTo -in @('Console', 'All'))
{
switch ($type)
{
"Info" { Write-Information $Message -InformationAction Continue; break }
"Warning" { Write-Warning $Message -WarningAction Continue; break }
"Error" { Write-Host "ERROR: $Message" -ForegroundColor Red; break }
"Verbose" { Write-Verbose $Message; break }
"Debug" { Write-Debug $Message; break }
}
}
($WriteTo -in @('Log', 'All'))
{
switch ($type)
{
"Info" { Add-Content -LiteralPath $Path -Value $content -Encoding UTF8; break }
"Warning" { Add-Content -LiteralPath $Path -Value $content -Encoding UTF8; break }
"Error" { Add-Content -LiteralPath $Path -Value $content -Encoding UTF8; break }
"Verbose" { if ($script:PSBoundParameters['Verbose']) { Add-Content -LiteralPath $Path -Value $content -Encoding UTF8; break } }
"Debug" { if ($script:PSBoundParameters['Debug']) { Add-Content -LiteralPath $Path -Value $content -Encoding UTF8; break } }
}
}
}
}
catch
{
Write-Output "$(Get-Date -UFormat '%Y.%m.%d %T')" + "`t" + "$Message"
}
} # Write-SimpleLog
#endregion FUNCTIONS
#region DECLARATION
if ($PSBoundParameters['Debug']) { $DebugPreference = 'Continue' }
# --- Setup log file
$LogFile = "$PSScriptRoot\MyTestlog.log"
# Write to console and log if debugging
if ($PSBoundParameters['Debug'])
{
$LogParams = New-SimpleLog -LogFile $LogFile -WriteTo All -WhatIf:$false
} else {
$LogParams = New-SimpleLog -LogFile $LogFile -WriteTo Log -WhatIf:$false
}
# or use the line below to use the default values (a generated log file name in the user's TEMP folder and write to the log file only)
#$LogParams = New-SimpleLog
#endregion DECLARATION
#region TESTING
Write-SimpleLog @LogParams -Message "This is an Information message ✓"
Write-SimpleLog @LogParams -Message "This is a Debug message 🚧" -Type Debug
Write-SimpleLog @LogParams -Message "This is a Verbose message 📖" -Type Verbose
Write-SimpleLog @LogParams -Message "This is a Warning message ⚠️" -Type Warning
Write-SimpleLog @LogParams -Message "This is an Error message ✗" -Type Error
#region TESTING
@dancing-groot

dancing-groot commented Apr 27, 2026

Copy link
Copy Markdown
Author

2026.04.27
Added support for -WhatIf
If -WhatIf is called for this function, pass it on to Write-SimpleLog
E.g. you may be running the script with -WhatIf, but if you still need to write to the console and/or log use
New-SimpleLog [parameters] -WhatIf:$false

Added Unicode support
Text written is now forced as UTF8 with BOM to support unicode emojis.
Note that Configuration Manager Trace Log Tool will not show unicode characters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment