PowerTip: Use PowerShell to Troubleshoot PowerShell

PowerTip: Use PowerShell to Troubleshoot PowerShell

  • Comments 2
  • Likes

Summary: Use Windows PowerShell to troubleshoot errors with Windows PowerShell.

Hey, Scripting Guy! Question How can I use Windows PowerShell to troubleshoot issues with Windows PowerShell?

Hey, Scripting Guy! Answer Use the Get-WinEvent cmdlet to parse the Windows PowerShell log. For example, to find all errors in a Windows PowerShell log:

In Windows PowerShell 3.0:

Get-WinEvent -LogName *powershell* | where leveldisplayname -eq 'error'

In Windows PowerShell 2.0:

Get-WinEvent -LogName *powershell* | where {$_.LevelDisplayName -eq 'error'}

You can also group by the error ID to get an idea of how often a particular error appears:

In Windows PowerShell 3.0:

Get-WinEvent -LogName *powershell* | where leveldisplayname -eq 'error' | group id

In Windows PowerShell 2.0:

Get-WinEvent -LogName *powershell* | where {$_.LevelDisplayName -eq 'error'} | group id

 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • This is very much faster and can be more easily tuned:

    Get-WinEvent -FilterHashtable @{logname='*PowerShell*';Level=2} -MaxEvents 10

    If the first error is a week ago it will take a long time to retrieve it.  Using this method we can get the last 10 errors instantly.

  • Performance example from local Windows 8.  

    Based on milliseconds the HashTable version is 463 times faster.

    PS C:\scripts> 65931.6117/142.1394

    463.851765942448

    PS C:\scripts> Measure-command {Get-WinEvent -FilterHashtable @{logname='*PowerShell*';Level=2} }

    Seconds           : 0

    Milliseconds      : 383

    TotalMinutes      : 0.00638664

    TotalSeconds      : 0.3831984

    TotalMilliseconds : 383.1984

    PS C:\scripts> Measure-Command{ Get-WinEvent -LogName *powershell* | where leveldisplayname -eq 'error' }

    Seconds           : 5

    Milliseconds      : 931

    TotalMinutes      : 1.098860195

    TotalSeconds      : 65.9316117

    TotalMilliseconds : 65931.6117