locked
Powershell, help comparing 2 variables values RRS feed

  • Question

  • Hi everyone

    I'm a powershell noob and im trying to create a script that can help me compare some citrix profile folders that are name SAM_xxxxx+x+xx+xxx along those lines with our current users in AD, in my mind a pretty simple script, however i can't make it work.

    $ADusers = Get-ADUser -SearchBase “OU=Users,OU=xxx,DC=xx,DC=xxx” -Filter * | select SamAccountName | Sort-Object SamAccountName
    $folders = get-childitem -Path \\xxxxx\xxxx -Directory
    $temp = "false"


    foreach ($folder in $folders)
    {
    foreach ($i in $ADuser.samaccountname)
    {
    if($folder.Name -like $i){
    $temp = "true"
    break
    }
    else {
    $temp = "false"
    }
    if($temp -eq "true"){
    write $i
    }
    }
    }

    Pretty sure its my $folder.name -like $i statement thats wrong, however i can't figure out how to fix it.

    I also thought about deleting everything after _ in the $folder.name but i couldn't get that to work either

    I would be very gratefull if anyone could help me shed some light on this problem.

    Thanks in advance, Hidin

    Monday, February 17, 2020 2:40 PM

All replies

  • Are you sure that the sAMAccountName of users exactly matche the name of the folders? If not then asterisks would help if it is not an exact matching: https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/escape-wildcards

    This posting is provided AS IS with no warranties or guarantees , and confers no rights.

    Ahmed MALEK

    My Website Link

    My Linkedin Profile

    My MVP Profile

    Monday, February 17, 2020 3:00 PM
  • Thanks for the reply

    The SAM account name is not like the folders. The sam accounts are named "ABC" and the folders are named "ABC_XXX+XXX+64X". I'm not sure what syntax to use to get around this, atleast the things i've tried havn't worked. For example if i try 

    if($folder.Name -like '$i*')

    or

    if($folder.Name -like '`$i*')

    It doesn't work


    • Edited by HidinDK Monday, February 17, 2020 3:23 PM
    Monday, February 17, 2020 3:21 PM
  • Use $foldername -match $samaccountname.


    \_(ツ)_/

    Monday, February 17, 2020 7:01 PM
  •  if($folder.Name -like "$i*") 

    you should use double quotes


    my blog: http://shserg.ru/

    Wednesday, February 19, 2020 7:33 AM