{"id":10658,"date":"2025-03-11T09:00:58","date_gmt":"2025-03-11T09:00:58","guid":{"rendered":"https:\/\/cheapwindowsvps.com\/blog\/a-comprehensive-guide-to-writing-logs-to-the-windows-event-viewer-using-powershell-and-cmd\/"},"modified":"2025-03-11T09:00:58","modified_gmt":"2025-03-11T09:00:58","slug":"a-comprehensive-guide-to-writing-logs-to-the-windows-event-viewer-using-powershell-and-cmd","status":"publish","type":"post","link":"https:\/\/cheapwindowsvps.com\/blog\/a-comprehensive-guide-to-writing-logs-to-the-windows-event-viewer-using-powershell-and-cmd\/","title":{"rendered":"A Comprehensive Guide to Writing Logs to the Windows Event Viewer Using PowerShell and CMD"},"content":{"rendered":"<p>To log event information directly to the Windows Event Viewer using PowerShell or Command Prompt, you can utilize the <strong>Write-EventLog<\/strong> cmdlet. This approach is beneficial for tracking script execution without creating text log files. Here\u2019s how you can do it:<\/p>\n<ol>\n<li>\n<p><strong>Log an Information Event<\/strong>:Use the following command to write an informational entry to the <em>Application<\/em> log:<\/p>\n<pre><code class=\"language-powershell\">Write-EventLog -LogName Application -Source &quot;Application&quot; -EntryType Information -EventID 1 -Message &quot;PS1 Script started&quot;<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>Add a Custom Event Source<\/strong>:If you want to use a separate event source in the log, create one with:<\/p>\n<pre><code class=\"language-powershell\">New-EventLog -LogName Application -Source &quot;MyScripts&quot;<\/code><\/pre>\n<p>Now you can log events with your custom source:<\/p>\n<pre><code class=\"language-powershell\">Write-EventLog -LogName Application -Source &quot;MyScripts&quot; -EntryType Warning -EventID 1 -Message &quot;PS1 Script started&quot;<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>Check the Event Viewer<\/strong>:Open the Event Viewer (<code>eventvwr.msc<\/code>), navigate to the <strong>Application<\/strong> log, and you should see the new event listed.<\/p>\n<\/li>\n<\/ol>\n<h3>Event Types<\/h3>\n<p>The <strong>EntryType<\/strong> parameter accepts the following types:<\/p>\n<ul>\n<li><code>Error<\/code><\/li>\n<li><code>Information<\/code><\/li>\n<li><code>FailureAudit<\/code><\/li>\n<li><code>SuccessAudit<\/code><\/li>\n<li><code>Warning<\/code><\/li>\n<\/ul>\n<ol start=\"4\">\n<li>\n<p><strong>Logging from CMD\/BAT Scripts<\/strong>:You can also log information via a command prompt script using <code>eventcreate.exe<\/code>:<\/p>\n<pre><code class=\"language-cmd\">eventcreate \/t information \/l application \/id 1 \/d &quot;BAT script started&quot;<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>Creating a Custom Log<\/strong>:If you need a custom log, use:<\/p>\n<pre><code class=\"language-powershell\">New-EventLog -LogName CustomPSLog -Source 'MyScripts','PSScript','PSLogonScript','PSSchedScript'<\/code><\/pre>\n<p>Before writing to it, check if the log exists:<\/p>\n<pre><code class=\"language-powershell\">If ([System.Diagnostics.EventLog]::SourceExists('CustomPSLog') -eq $False) {    New-EventLog -LogName CustomPSLog -Source &quot;MyScripts&quot;}<\/code><\/pre>\n<p>After creating the log, ensure it receives at least one event to appear in Event Viewer:<\/p>\n<pre><code class=\"language-powershell\">Write-EventLog -LogName CustomPSLog -Source MyScripts -EntryType Information -EventID 1 -Message &quot;Test&quot;<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>Finding Events with PowerShell<\/strong>:To filter and find specific events in your logs, use the <code>Get-WinEvent<\/code> cmdlet:<\/p>\n<pre><code class=\"language-powershell\">Get-WinEvent -FilterHashtable @{logname='CustomPSLog';id=1} | ft TimeCreated,Id,Message | Select-Object -First 5<\/code><\/pre>\n<\/li>\n<\/ol>\n<h4>Note on PowerShell Core<\/h4>\n<p>In PowerShell Core (7.x), the <strong>Write-EventLog<\/strong> cmdlet is not available, and you should use <strong>New-WinEvent<\/strong> instead, although it requires registering an event provider. For simplicity, it&#8217;s advisable to import the management module:<\/p>\n<pre><code class=\"language-powershell\">Import-Module Microsoft.PowerShell.Management -UseWindowsPowerShellWrite-EventLog -LogName CustomPSLog1 -Source CustomPSLog -EntryType Information -EventID 1 -Message &quot;Test2&quot;<\/code><\/pre>\n<p>Make sure to run these commands as an administrator, as only users in the local Administrators group can send events to the event logs created by administrators.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To log event information directly to the Windows Event Viewer using PowerShell or Command Prompt, you can utilize the Write-EventLog cmdlet. This approach is beneficial for tracking script execution without creating text log files. Here\u2019s how you can do it: Log an Information Event:Use the following command to write an informational entry to the Application [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":10659,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[117,99,108],"tags":[],"class_list":["post-10658","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","category-windows-11","category-windows-server-2022"],"_links":{"self":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/10658","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/comments?post=10658"}],"version-history":[{"count":0,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/10658\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/media\/10659"}],"wp:attachment":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/media?parent=10658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/categories?post=10658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/tags?post=10658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}