Reputation: 35
I am trying to search for files, but I am trying to limit the number of files I have to touch in my search because what I am looking for is in a specific subfolder. However, the subfolder is nested in the users folders on the servers
I've tried using Get-childitem \server\%username%\desktop\ -filter file.ini
Get-ChildItem -path \server\%username%\desktop\ -filter file.ini
it blows up at the username. I don't need to search all the above folders, just the specific subfolder but I can't seem to get around the user variable.
Upvotes: 1
Views: 3682
Reputation: 35
So, I had to get a little creative.
$path=\server\firstlvl\
'#this gives me the names of the usernamed subfolders
$getUserDirectory=Get-Childitem -path $path
foreach($f in $getUserDirectory){
$getMyFile=Get-ChildItem -path $path$f\Data -recurse -filter notes.ini
}
This gets me into the directory so that I can then begin to scan for the file and subsequently check its contents for what I need to find.
Upvotes: 0
Reputation: 8442
For locations like the desktop (so called 'special folders'), an alternative technique is to use .NET to find the location for you. For Example:
$desktop = [Environment]::GetFolderPath("Desktop")
Which will return something like this:
C:\Users\<UserName>\Desktop
An advantage is you don't need to know any part of the path already (e.g. the drive letter), just the specific folder you want. Disadvantage is that it only works locally, so you can't point it to a remote server and grab the value from their (though it should work through PowerShell Remoting).
Here is a list here of all the possible values you can use instead of 'Desktop':
Environment.SpecialFolder Enum
Or you can grab them programatically:
[Enum]::GetNames([Environment+SpecialFolder])
AdminTools
ApplicationData
CDBurning
CommonAdminTools
CommonApplicationData
CommonDesktopDirectory
CommonDocuments
CommonMusic
CommonOemLinks
CommonPictures
CommonProgramFiles
.
.
.
Upvotes: 0
Reputation: 51
Try using $env:username
instead of %username%
:
Get-childitem \server\$env:username\desktop\ -filter file.ini
Get-ChildItem -path \server\$env:username\desktop\ -filter file.ini
Upvotes: 1
Reputation: 36332
PowerShell handles environment variables slightly differently than batch files. Instead of %username%
you will want to reference $env:username
instead.
Get-ChildItem "\server\$env:username\desktop\" -filter 'file.ini'
Mind you, that will only search the current drive with the folder 'server' being in the root of the drive, so if your drive that you are working on is the C: drive, it will only check this path:
C:\server\<username>\desktop\file.ini
You may want to add the -recurse
switch to make it check subfolders, or take action to modify the path as needed. If you were intending to look on a remote server for the file, you may have wanted to do this:
Get-ChildItem "\\server\c$\users\$env:username\desktop\" -filter 'file.ini'
Upvotes: 0