Help
Help

Reputation: 181

Running PowerShell Command Directly On CMD Gives Index Was Out Of Range Error

The command below is designed to list expired user accounts:

powershell -c "Get-LocalUser | Where-Object { $_.AccountExpires -le (Get-Date) -and $null -ne $_.AccountExpires } | Select-Object Name, AccountExpires"

Running directly on PowerShell seems to run fine, however, when I run the same command via CMD, it gives the error below:

Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index').

I believe the issue is related to the $null parameter but I need this to exclude blank matches from the output. Does anyone know a way of fixing this error? Or an alternative method to no match blank output?

Upvotes: 0

Views: 152

Answers (2)

alexzelaya
alexzelaya

Reputation: 255

It might not be $null, try an empty string instead:

("" -ne $_.AccountExpires)

Upvotes: 1

Scepticalist
Scepticalist

Reputation: 3923

You should make things clear when using operators:

powershell -c "Get-LocalUser | Where-Object { ($_.AccountExpires -le (Get-Date)) -and ($null -ne $_.AccountExpires) } | Select-Object Name, AccountExpires"

This works fine for me

Upvotes: 1

Related Questions