locked
Using a Variable as a filter in a Where-Object RRS feed

  • Question

  • 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)

    Monday, July 8, 2019 9:41 PM

Answers

  • Where-Object {$_.Name -like "*$Version*"}

    Double quotes.

    help about_quoting


    \_(ツ)_/

    • Marked as answer by apetheo Monday, July 8, 2019 10:35 PM
    Monday, July 8, 2019 10:20 PM

All replies

  • Where-Object {$_.Name -like "*$Version*"}

    Double quotes.

    help about_quoting


    \_(ツ)_/

    • Marked as answer by apetheo Monday, July 8, 2019 10:35 PM
    Monday, July 8, 2019 10:20 PM
  • Okay that was an easy change. that fixed it. 


    -AAP (Andrew)

    Monday, July 8, 2019 10:35 PM