Powershell scripting on AD | how to filtering the output

Stefano Riccardi 21 Reputation points
2022-03-28T08:37:04.313+00:00

Hello,
please I need to help, to understand how to filter the output, I show you an example, I am using this simple script:

import-module ActiveDirectory
Get-ADUser -Filter {enabled -eq "true" -and SamAccountName -like "i*"}  | ForEach-Object {
   $usr = [ADSI]"LDAP://$($_.DistinguishedName)"
   $old_Profile_path = $usr.psbase.InvokeGet("terminalservicesProfilepath")
   If ($old_Profile_path -notlike ""){ 
      Write-Host $($_.SamAccountName) $old_Profile_path 
   }
   else
   {
     Write-Host $($_.SamAccountName) "ProfilePath empty"

   }
   } 

the output is like this:

IWT0038 \oldshare\ProfileDir$\IWT0038
IWT0064 \oldshare\ProfileDir$\IWT0064
IWT0087 \newshare\farm_xd_mt$\IST\PROFILE_DIR\IW\IWT0087

my scope it is to obtain only the "newshare" rows, I tried the findstr unsuccesfully.
I am not a very skilled scripter, thank you very much for the help
regards

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,244 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,462 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,906 Reputation points
    2022-03-28T14:38:14.603+00:00

    Change line #5 and include another test inside your check for an empty value:

    If ($old_Profile_path -notlike ""){
        if ($old_Profile_path -like "*") { 
            Write-Host $($_.SamAccountName) $old_Profile_path
        }
    }
    

    This assumes you still want to report an empty value.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Stefano Riccardi 21 Reputation points
    2022-04-04T09:40:28.67+00:00

    thank you Rich

    0 comments No comments