Cannot use a variable in where clause
-
12-01-2021 - |
Question
I am using the PnP SharePoint powershell moudle and trying to filter a users list based on a variable.
Currently here is what I have:
$RootFolder = '/Path/To/Folder'
$folders = Get-PnPFolderItem -FolderSiteRelativeUrl $RootFolder
foreach ($folder in $folders){
$user = Get-PnPUser | Where Title -Like "*$folder.Name*"}
}
This does not return results. However, if i change | Where Title -Like "*username*"
it works fine.
The user name format is "FirstName LastName (FLastName)" and i am trying to filter based on "FLastName".
I tried single quotes and also created a variable $username = "*" +$folder.Name+ "*"
and used that variable instead but no difference.
Any thoughts would be appreciated.
La solution
Try this:
Where Title -Like "*$( $folder.Name )*"
The variable expansion/replacement ends with the "." in a simple string.
Autres conseils
Edited to use Mike Smith's solution.
I was able to get this to work by doing the following adjustment (adding string to the variable)
$RootFolder = '/Path/To/Folder'
$folders = Get-PnPFolderItem -FolderSiteRelativeUrl $RootFolder
foreach ($folder in $folders){
$user = Get-PnPUser | Where Title -Like "*$($folder.Name)*"}
}