0

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

1 Answer 1

4

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" })
Sign up to request clarification or add additional context in comments.

2 Comments

$_.CreationTime.Year -eq 2016 avoids the conversion to string and the use of regex, and uhh .. the possibility for false positive matches should the script still be running after the year 20160 ;)
@TessellatingHeckler You beat me for the very same comment. (except the year 20160 :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.