NebulousNifty
NebulousNifty

Reputation: 35

Using wildcards in file path

I need to search a server for some files... but the file in question is buried in a folder that is under user IDs. So I am trying to wildcard %username% to get to the folder I want to search.

Tired using %username% and * to wildcard the upper folder to get through to the subfolder.

$Path="\\server\profiles\%username%\Data\"
$filename='notes.ini'

Get-ChildItem -Path $Path -Recurse -Filter $filename -ErrorAction SilentlyContinue

I'm hoping to just search the Data folder for the notes.ini file and not having to search all the folders in between.

Upvotes: 0

Views: 6785

Answers (2)

js2010
js2010

Reputation: 27428

%username% in powershell is $env:username

%username% isn't a wildcard, it's an environment variable in cmd

Upvotes: 0

Theo
Theo

Reputation: 61028

If you do not know the name of the user, you can use the wildcard asteriks * in the path

$Path     = "\\server\profiles\*\Data\"  #"# use wildcard to search all user folders 
$filename = 'notes.ini'

# this returns the full file and path names of the file you are looking for:
Get-ChildItem -Path $Path -Recurse -Filter $filename -File -ErrorAction SilentlyContinue | Select-Object -ExpandProperty FullName

If this is not what you are after, please add some more explanation in your question.

Upvotes: 1

Related Questions