Daystar1124
Daystar1124

Reputation: 63

Simple powershell script using Get-ChildItem fails

So I have a process that looks for specific files like below:

@(Get-ChildItem -Path \\$Server\c$\temp\* where-object { $_.CreationTime.Date -match "2016" })

This worked just fine until I wanted to try and split my output up. I learned about "$OFS=" "" and wanted to use that (global powershell variable?) and changed it to $OFS="`r`n"..

Now when I run the command above, I receive an error:

Get-ChildItem : A positional parameter cannot be found that accepts argument ' $_.CreationTime.Date -match "2016" '.

I tried resetting it with $OFS=" " and that doesn't seem to help. I can't figure out what I've done to ruin my simple script. Sorry for the very noob question but I've been searching this for ages and cannot come up with an answer. Everything I find online seems to state that my powershell command should work

Upvotes: 0

Views: 1034

Answers (1)

Olaf Reitz
Olaf Reitz

Reputation: 694

Your powershell command can't work. Get-ChildItem and Where-Object are different Cmdlets, try to pipe (|) the output from Get-ChildItem to Where-Object

@(Get-ChildItem -Path \\$Server\c$\temp\* | where-object { $_.CreationTime.Date -match "2016" })

Upvotes: 4

Related Questions