Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Using a Variable as a filter in a Where-Object

$
0
0

I have been trying to figure this out... I have a folder structure that I need to be able to pull up contents of based on user input. I have a folder named "Folder1" (for this purpose). and inside Folder1 are different folders named something like "Update_3657_June2019", "Update_3578_May2019" I also have parameters set to be able to have the user input the update number ($Version). So when the user uses "Update.ps1 -Version 3657" I want it to be able to go to the path with these folders and grab the content of that specific Update folder by looking for the folder with the $Version specified.  

This below does not work. How do I get the Where-Object to filter on a variable?

Param(
    [String]$Version
)

$TopPath="\\SERVER1\Folder1"
$AllDirs=Get-ChildItem -Path $NewPath -Directory
$Folder=($AllDirs | Where-Object {$_.Name -like '*$Version*'}).Name
$FullPath=$TopPath\$Folder
Get-ChildItem -Path $FullPath


-AAP (Andrew)


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>