Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 122
  • Last Modified:

Add a variable to property in Powershell

Good Day,

This should be very quick and easy.

I have a powershell script and we have multiple child domains.  I would like to include a new property for which domain the ADUser object is in as part of the output.

Below is my script:
__________________________________________________________
$z='y'
$domains = (Get-ADForest).Domains

While ($z -eq 'y')
    {
    $userx = @()
    $nameregex = Read-Host "put in part or al of a user's name"
    $userd = '*'+$nameregex+'*'
        foreach ($domain in $domains) {
            if ($domain -ne "cst.is.local" ) {
                Write-Output "Searching Domain " $domain
               $userx += Get-ADUser -Filter {name -like $userd} -server $domain | Select-Object name,samaccountname,enabled,DistinguishedName
             }
        }
        Write-output $userx | ft -AutoSize
    $z = Read-Host 'do you want to find another user?'
    $z = $z.ToLower()
    }

________________________________________________________________
The script works great but I would like to add the $domain variable to the Select-object output.

Thanks in advance.
0
mrfite
Asked:
mrfite
  • 2
1 Solution
 
it_saigeDeveloperCommented:
Like this:
$z='y'
$domains = (Get-ADForest).Domains

While ($z -eq 'y')
    {
    $userx = @()
    $nameregex = Read-Host "put in part or al of a user's name"
    $userd = '*'+$nameregex+'*'
        foreach ($domain in $domains) {
            if ($domain -ne "cst.is.local" ) {
                Write-Output "Searching Domain " $domain
               $userx += Get-ADUser -Filter {name -like $userd} -server $domain | Select-Object name,samaccountname,enabled,DistinguishedName,@{l="Domain";e={$domain}}
             }
        }
        Write-output $userx | ft -AutoSize
    $z = Read-Host 'do you want to find another user?'
    $z = $z.ToLower()
    }

Open in new window


-saige-
0
 
mrfiteAuthor Commented:
Thanks,

I knew it was something simple.
0
 
mrfiteAuthor Commented:
Thanks again
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now