How can i use powershell to tail a specific windows event log? Is it possible?
The Tail command is popular in the Unix language and it is used to retrieve the specific number of lines from the end of the document or the log files. PowerShell doesn't have the command with the same name but from the PowerShell v3. 0 onwards, PowerShell has added -Tail parameter in the Get-Content cmdlet.
Open the Windows Event Viewer: press Windows R , type eventvwr. msc and press Enter . Scroll down to Application and Service Logs , Microsoft , Windows , WFP . Right-click on a log process and select Disable Log .
Viewing the Windows PowerShell Event Log To examine the events and their properties, use the Sort-Object cmdlet, the Group-Object cmdlet, and the cmdlets that contain the Format verb (the Format cmdlets). For more information, type "Get-Help Get-EventLog" and "Get-Help Get-WmiObject".
An advanced tail -f command with GUI, MakeLogic Tail is the tail for Windows. It can be used to monitor the log files of various servers and comes with a variety of other intuitive and useful features.
Open it with notepad $PROFILE. Then in the text document, create a new function: function Tail ($path) { Get-content -tail 15 -path $path -wait } This way you can access the function each time you start PowerShell. This should be the accepted answer.
I've done this on occasion:
$idx = (get-eventlog -LogName System -Newest 1).Index
while ($true)
{
start-sleep -Seconds 1
$idx2 = (Get-EventLog -LogName System -newest 1).index
get-eventlog -logname system -newest ($idx2 - $idx) | sort index
$idx = $idx2
}
Per MSDN docs:
Get-WinEvent
is designed to replace theGet-EventLog
cmdlet on computers running Windows Vista and later versions of Windows.Get-EventLog
gets events only in classic event logs.Get-EventLog
is retained in Windows PowerShell for backward compatibility.
And spurred on by my own need to tail a non-classic event log (would that be an event log nouveau perchance?) here is the wonderfully concise code of @mjolinor repurposed to use Get-WinEvent
:
Set-PSDebug -Strict
function Get-WinEventTail($LogName, $ShowExisting=10) {
if ($ShowExisting -gt 0) {
$data = Get-WinEvent -provider $LogName -max $ShowExisting
$data | sort RecordId
$idx = $data[0].RecordId
}
else {
$idx = (Get-WinEvent -provider $LogName -max 1).RecordId
}
while ($true)
{
start-sleep -Seconds 1
$idx2 = (Get-WinEvent -provider $LogName -max 1).RecordId
if ($idx2 -gt $idx) {
Get-WinEvent -provider $LogName -max ($idx2 - $idx) | sort RecordId
}
$idx = $idx2
# Any key to terminate; does NOT work in PowerShell ISE!
if ($Host.UI.RawUI.KeyAvailable) { return; }
}
}
I added in a few bells and whistles for convenience:
ShowExisting
parameter.Get-WinEvent
's default) due to the natural order that tail requires.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With