Wildcards in variable path with Powershell

robinwood13 picture robinwood13 · Aug 3, 2015 · Viewed 14.1k times · Source

I would like in my script to use wildcard in variable like this :

$TARGET = "\\MACHINE1\c$\ProgramData\Test\12.*\Data\"

The problem is $TARGET returns

\\MACHINE1\c$\ProgramData\Test\12.*\Data\

and not

\\MACHINE1\c$\ProgramData\Test\12.1.1.1\Data\

However

Test-Path "\\MACHINE1\c$\ProgramData\Test\12.*\Data\"

=> TRUE

Thanks for your help

Answer

Avshalom picture Avshalom · Aug 3, 2015

The Best in this cases IMO is using Resolve-Path,

$TARGET = Resolve-Path "\\MACHINE1\c$\ProgramData\Test\12.*\Data\" | Select -ExpandProperty Path