powershell - Using a wildcard in a variable for Get-ChildItem filter -


below code

$now = [system.datetime]::now  $filter = $now.tostring("mm_dd_yyyy") + "_*_s1.txt" write-host $filter  get-childitem -filter $filter | % {write-host $_}# select-object -outvariable $files  write-host $files.gettype() 

i can take output of 'write-host $filter' statement , paste powershell , results expect, know filter correct. because i'm using variable in get-childitem call? how 1 go doing this.

you're misusing -outvariable. it's expecting name of variable without $. should using select-object -outvariable files.

but code un-powershell-y. using pipeline & select-object unnecessary here. try instead.

$filter = $(get-date -f "mm_dd_yyyy") + "_*_s1.txt"; $files = get-childitem -filter $filter; 

Comments