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

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
PowerShellWindows 11Windows Server 2022
previous post
VMware Workstation: Slow VMs Performance on Windows
next post
AD Domain Join: Computer Account Re-use Blocked

Related Reading

How to Assign (Passthrough) a Physical GPU to...

June 11, 2024

Extend an Expired User Password in Active Directory

December 23, 2024

Adding ESXi Host to VMware vCenter Server (vCSA)

March 12, 2024

Check the Software Installation/Removal History in Windows

October 8, 2024

How to Add or Remove Pinned Folders to...

August 11, 2024

Configure File and Folder Access Auditing on Windows...

July 8, 2024

How to Create, Delete, and Manage System Restore...

March 14, 2024

How to Enable and Configure Wake-on-LAN (WoL) in...

April 1, 2024

Leave a Comment Cancel Reply

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

Recent Posts

  • Failed to Open the Group Policy Object on a Computer

    June 2, 2025
  • Disable the Lock Screen Widgets in Windows 11

    May 26, 2025
  • Configuring Windows Protected Print Mode (WPP)

    May 19, 2025
  • 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

Follow us

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

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


Back To Top