Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wildcards in variable path with Powershell

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

like image 951
robinwood13 Avatar asked Aug 03 '15 11:08

robinwood13


2 Answers

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

$TARGET = Resolve-Path "\\MACHINE1\c$\ProgramData\Test\12.*\Data\" | Select -ExpandProperty Path
like image 108
Avshalom Avatar answered Sep 26 '22 19:09

Avshalom


You should use Get-ChildItem to retrieve the real path:

$TARGET = "\\MACHINE1\c$\ProgramData\Test\12.*\Data\"
Get-ChildItem $Target


    Directory: \\MACHINE1\c$\ProgramData\Test\12.1.1.1

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----        10-10-2014     12:48            data
like image 30
Johan de Haan Avatar answered Sep 26 '22 19:09

Johan de Haan