Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux

 Windows OS Hub / PowerShell / How to Write Logs to the Windows Event Viewer from PowerShell/CMD

March 11, 2025 PowerShellWindows 11Windows Server 2022

How to Write Logs to the Windows Event Viewer from PowerShell/CMD

Alternatively to using text log files in scripts, you can write event information directly to the Event Viewer logs. In this article, we’ll look at how to write logs to the Windows Event Viewer from a PowerShell script or the command prompt.

To write information to the Windows event logs, use the Write-EventLog cmdlet. For example, to write an information event to the Application log:

Write-EventLog -LogName Application -Source "Application" -EntryType Information -EventID 1 -Message "PS1 Script started"

You can add a separate event source to an existing log:

New-EventLog -LogName Application -Source "MyScripts"

Now, you can write events with a custom source:

Write-EventLog -LogName Application -Source "MyScripts" -EntryType Warning –EventID 1 –Message "PS1 Script started"

Open the Event Viewer console (eventvwr.msc), expand the Application log, and check that a new event with your description has been added to the log.

Write-EventLog - PowerShell

The following event types can be used in the EntryType parameter: Error, Information, FailureAudit, SuccessAudit, or Warning.

To add an event to the log from a BAT/CMD script, use the eventcreate.exe command:

eventcreate /t information /l application /id 1 /d "BAT script started"

eventcreate.exe - add ta custom event in a specified event log

You can create a custom classic event log in Event Viewer using the New-EventLog command.

New-EventLog -LogName CustomPSLog -source 'MyScripts','PSScript','PSLogonScript','PSSchedScript'


If you are writing events to a custom log, you should first check in the script to see if the log already exists.
If ([System.Diagnostics.EventLog]::SourceExists('CustomPSLog') -eq $False) {
New-EventLog -LogName CustomPSLog -Source ...
}

For a new classic event log to appear in the Event Viewer graphical console, you must send at least one event to it.

Write-EventLog -LogName CustomPSLog -Source MyScripts -EntryType Information -EventID 1 -Message "Test"

A new log will appear in the root of the Applications and Services Logs section. A new EVTX file will be created for the log file in the %SystemRoot%\System32\Winevt\Logs folder.

Creating a custom Windows log in Event Viewer

Learn how to configure the maximum event log size and other options on Windows.

Use the Get-WinEvent cmdlet to find and filter events in Event Viewer logs:

Get-WinEvent -FilterHashtable @{logname='CustomPSLog';id=1}|ft TimeCreated,Id,Message | Select-Object -First 5

Creating Get-WinEvent queries

In the latest versions of PowerShell Core, the Write-EventLog cmdlet is not supported. If you try to run a command that contains it, you will get an error:

Write-EventLog: The term 'Write-EventLog' is not recognized as a name of a cmdlet, function, script file, or executable program.

In PowerShell Core 7.x, you should use New-WinEvent instead. However, to use it, you need to register a separate event provider, which can be complicated. In PowerShell Core scripts, it is much easier first to import the Microsoft.PowerShell.Management module by using the -UseWindowsPowerShell option. Then, you can use the Write-EventLog cmdlet in your PowerShell Core scripts:

Import-Module Microsoft.PowerShell.Management -UseWindowsPowerShell
Write-EventLog -LogName CustomPSLog1 -Source CustomPSLog -EntryType Information -EventID 1 -Message "Test2"

To use the Write-EventLog cmdlet to add logs to the Event Viewer, use the account that is a member of the local Administrators group. A non-admin user can only send events to custom Event Viewer logs created by an administrator.

0 comment
3
Facebook Twitter Google + Pinterest
previous post
VMware Workstation: Slow VMs Performance on Windows
next post
AD Domain Join: Computer Account Re-use Blocked

Related Reading

Unable to Map Drive: An extended error has...

May 13, 2025

Map a Network Drive over SSH (SSHFS) in...

May 13, 2025

Configure NTP Time Source for Active Directory Domain

May 6, 2025

How to Cancel Windows Update Pending Restart Loop

May 6, 2025

View Windows Update History with PowerShell (CMD)

April 30, 2025

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMware
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Map a Network Drive over SSH (SSHFS) in Windows

    May 13, 2025
  • Configure NTP Time Source for Active Directory Domain

    May 6, 2025
  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025
  • AD Domain Join: Computer Account Re-use Blocked

    March 11, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • How to Assign (Passthrough) a Physical GPU to a Hyper-V Virtual Machine
  • Run PowerShell Scripts on a Schedule with Task Scheduler
  • Check Windows 11 Hardware Readiness with PowerShell Script
  • Extend an Expired User Password in Active Directory
  • How to Add or Remove Pinned Folders to Quick Access with PowerShell and GPO
  • How to Enable and Configure Wake-on-LAN (WoL) in Windows
  • How to Find Windows Version and Build Number Installed
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top