Remove certain characters from write-host output in Powershell (Get-ADuser)










1














I'm comparing users between two domains to make sure users that are disabled in one, are disabled in the other, using these two steps:



Domain 1:



Get-ADUser -SearchBase "OU=ou2,OU=ou1,DC=pre,DC=domain1,DC=com" -Filter * -Properties * | Select-Object Name | Export-Csv -encoding "utf8" Users.csv


Domain 2:



$input = import-csv -path "Users.csv" 
ForEach ($User in $input)
$result = get-aduser -SearchBase "OU=ou2,OU=ou1,DC=pre,DC=domain2,DC=com" -Filter "name -eq '$($User.Name)'"


This works, but gives me the following output:



Name Enabled
---- -------
Firstname1 Lastname1 True
@Name=Firstname2 Lastname2 - Name not found. Please do a manual check


How do I remove "@Name=" and ""?
I have tried adding -ExtendProperity to $result, and Replace with no luck. I'm probably doing it wrong..










share|improve this question





















  • @Name=Firstname2 Lastname2 is an array and not a string value.
    – TobyU
    Nov 12 at 14:24






  • 1




    @TobyU: It is the string representation of a custom object, not of an array.
    – mklement0
    Nov 12 at 14:32






  • 1




    Also, don't do -Properties * if all you are after are the Name and Enabled properties. These two properties are returned by default, as are DistinguishedName, GivenName, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName, so you can skip the -Properties * alltogether.
    – Theo
    Nov 12 at 14:35
















1














I'm comparing users between two domains to make sure users that are disabled in one, are disabled in the other, using these two steps:



Domain 1:



Get-ADUser -SearchBase "OU=ou2,OU=ou1,DC=pre,DC=domain1,DC=com" -Filter * -Properties * | Select-Object Name | Export-Csv -encoding "utf8" Users.csv


Domain 2:



$input = import-csv -path "Users.csv" 
ForEach ($User in $input)
$result = get-aduser -SearchBase "OU=ou2,OU=ou1,DC=pre,DC=domain2,DC=com" -Filter "name -eq '$($User.Name)'"


This works, but gives me the following output:



Name Enabled
---- -------
Firstname1 Lastname1 True
@Name=Firstname2 Lastname2 - Name not found. Please do a manual check


How do I remove "@Name=" and ""?
I have tried adding -ExtendProperity to $result, and Replace with no luck. I'm probably doing it wrong..










share|improve this question





















  • @Name=Firstname2 Lastname2 is an array and not a string value.
    – TobyU
    Nov 12 at 14:24






  • 1




    @TobyU: It is the string representation of a custom object, not of an array.
    – mklement0
    Nov 12 at 14:32






  • 1




    Also, don't do -Properties * if all you are after are the Name and Enabled properties. These two properties are returned by default, as are DistinguishedName, GivenName, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName, so you can skip the -Properties * alltogether.
    – Theo
    Nov 12 at 14:35














1












1








1







I'm comparing users between two domains to make sure users that are disabled in one, are disabled in the other, using these two steps:



Domain 1:



Get-ADUser -SearchBase "OU=ou2,OU=ou1,DC=pre,DC=domain1,DC=com" -Filter * -Properties * | Select-Object Name | Export-Csv -encoding "utf8" Users.csv


Domain 2:



$input = import-csv -path "Users.csv" 
ForEach ($User in $input)
$result = get-aduser -SearchBase "OU=ou2,OU=ou1,DC=pre,DC=domain2,DC=com" -Filter "name -eq '$($User.Name)'"


This works, but gives me the following output:



Name Enabled
---- -------
Firstname1 Lastname1 True
@Name=Firstname2 Lastname2 - Name not found. Please do a manual check


How do I remove "@Name=" and ""?
I have tried adding -ExtendProperity to $result, and Replace with no luck. I'm probably doing it wrong..










share|improve this question













I'm comparing users between two domains to make sure users that are disabled in one, are disabled in the other, using these two steps:



Domain 1:



Get-ADUser -SearchBase "OU=ou2,OU=ou1,DC=pre,DC=domain1,DC=com" -Filter * -Properties * | Select-Object Name | Export-Csv -encoding "utf8" Users.csv


Domain 2:



$input = import-csv -path "Users.csv" 
ForEach ($User in $input)
$result = get-aduser -SearchBase "OU=ou2,OU=ou1,DC=pre,DC=domain2,DC=com" -Filter "name -eq '$($User.Name)'"


This works, but gives me the following output:



Name Enabled
---- -------
Firstname1 Lastname1 True
@Name=Firstname2 Lastname2 - Name not found. Please do a manual check


How do I remove "@Name=" and ""?
I have tried adding -ExtendProperity to $result, and Replace with no luck. I'm probably doing it wrong..







powershell compare






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 14:20









Erik M.

365




365











  • @Name=Firstname2 Lastname2 is an array and not a string value.
    – TobyU
    Nov 12 at 14:24






  • 1




    @TobyU: It is the string representation of a custom object, not of an array.
    – mklement0
    Nov 12 at 14:32






  • 1




    Also, don't do -Properties * if all you are after are the Name and Enabled properties. These two properties are returned by default, as are DistinguishedName, GivenName, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName, so you can skip the -Properties * alltogether.
    – Theo
    Nov 12 at 14:35

















  • @Name=Firstname2 Lastname2 is an array and not a string value.
    – TobyU
    Nov 12 at 14:24






  • 1




    @TobyU: It is the string representation of a custom object, not of an array.
    – mklement0
    Nov 12 at 14:32






  • 1




    Also, don't do -Properties * if all you are after are the Name and Enabled properties. These two properties are returned by default, as are DistinguishedName, GivenName, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName, so you can skip the -Properties * alltogether.
    – Theo
    Nov 12 at 14:35
















@Name=Firstname2 Lastname2 is an array and not a string value.
– TobyU
Nov 12 at 14:24




@Name=Firstname2 Lastname2 is an array and not a string value.
– TobyU
Nov 12 at 14:24




1




1




@TobyU: It is the string representation of a custom object, not of an array.
– mklement0
Nov 12 at 14:32




@TobyU: It is the string representation of a custom object, not of an array.
– mklement0
Nov 12 at 14:32




1




1




Also, don't do -Properties * if all you are after are the Name and Enabled properties. These two properties are returned by default, as are DistinguishedName, GivenName, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName, so you can skip the -Properties * alltogether.
– Theo
Nov 12 at 14:35





Also, don't do -Properties * if all you are after are the Name and Enabled properties. These two properties are returned by default, as are DistinguishedName, GivenName, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName, so you can skip the -Properties * alltogether.
– Theo
Nov 12 at 14:35













1 Answer
1






active

oldest

votes


















3














$User is a custom object (type [pscustomobject], as output by Import-Csv), and @Name=Firstname2 Lastname2 is its stringified representation[1], because Write-Host stringifies its arguments for display.



Access the .Name property instead to get just the name:



Write-host -ForegroundColor Yellow $User.Name "- Name not found. Please do a manual check" 


More idiomatically, using a single expandable string (string interpolation inside "..."):



Write-host -ForegroundColor Yellow "$($User.Name) - Name not found. Please do a manual check" 


If you want to include the full object representation as it would appear if you printed it directly to the console, you need Out-String, but note that you'll end up with multi-line output:



Write-host -ForegroundColor Yellow "$($User | Out-String) - Name not found. Please do a manual check" 



[1] You can verify this as follows: $user = [pscustomobject] @ Name = 'Firstname1 LastName1' ; "$user". The output is string @Name=Firstname1 LastName1.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264123%2fremove-certain-characters-from-write-host-output-in-powershell-get-aduser%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    $User is a custom object (type [pscustomobject], as output by Import-Csv), and @Name=Firstname2 Lastname2 is its stringified representation[1], because Write-Host stringifies its arguments for display.



    Access the .Name property instead to get just the name:



    Write-host -ForegroundColor Yellow $User.Name "- Name not found. Please do a manual check" 


    More idiomatically, using a single expandable string (string interpolation inside "..."):



    Write-host -ForegroundColor Yellow "$($User.Name) - Name not found. Please do a manual check" 


    If you want to include the full object representation as it would appear if you printed it directly to the console, you need Out-String, but note that you'll end up with multi-line output:



    Write-host -ForegroundColor Yellow "$($User | Out-String) - Name not found. Please do a manual check" 



    [1] You can verify this as follows: $user = [pscustomobject] @ Name = 'Firstname1 LastName1' ; "$user". The output is string @Name=Firstname1 LastName1.






    share|improve this answer



























      3














      $User is a custom object (type [pscustomobject], as output by Import-Csv), and @Name=Firstname2 Lastname2 is its stringified representation[1], because Write-Host stringifies its arguments for display.



      Access the .Name property instead to get just the name:



      Write-host -ForegroundColor Yellow $User.Name "- Name not found. Please do a manual check" 


      More idiomatically, using a single expandable string (string interpolation inside "..."):



      Write-host -ForegroundColor Yellow "$($User.Name) - Name not found. Please do a manual check" 


      If you want to include the full object representation as it would appear if you printed it directly to the console, you need Out-String, but note that you'll end up with multi-line output:



      Write-host -ForegroundColor Yellow "$($User | Out-String) - Name not found. Please do a manual check" 



      [1] You can verify this as follows: $user = [pscustomobject] @ Name = 'Firstname1 LastName1' ; "$user". The output is string @Name=Firstname1 LastName1.






      share|improve this answer

























        3












        3








        3






        $User is a custom object (type [pscustomobject], as output by Import-Csv), and @Name=Firstname2 Lastname2 is its stringified representation[1], because Write-Host stringifies its arguments for display.



        Access the .Name property instead to get just the name:



        Write-host -ForegroundColor Yellow $User.Name "- Name not found. Please do a manual check" 


        More idiomatically, using a single expandable string (string interpolation inside "..."):



        Write-host -ForegroundColor Yellow "$($User.Name) - Name not found. Please do a manual check" 


        If you want to include the full object representation as it would appear if you printed it directly to the console, you need Out-String, but note that you'll end up with multi-line output:



        Write-host -ForegroundColor Yellow "$($User | Out-String) - Name not found. Please do a manual check" 



        [1] You can verify this as follows: $user = [pscustomobject] @ Name = 'Firstname1 LastName1' ; "$user". The output is string @Name=Firstname1 LastName1.






        share|improve this answer














        $User is a custom object (type [pscustomobject], as output by Import-Csv), and @Name=Firstname2 Lastname2 is its stringified representation[1], because Write-Host stringifies its arguments for display.



        Access the .Name property instead to get just the name:



        Write-host -ForegroundColor Yellow $User.Name "- Name not found. Please do a manual check" 


        More idiomatically, using a single expandable string (string interpolation inside "..."):



        Write-host -ForegroundColor Yellow "$($User.Name) - Name not found. Please do a manual check" 


        If you want to include the full object representation as it would appear if you printed it directly to the console, you need Out-String, but note that you'll end up with multi-line output:



        Write-host -ForegroundColor Yellow "$($User | Out-String) - Name not found. Please do a manual check" 



        [1] You can verify this as follows: $user = [pscustomobject] @ Name = 'Firstname1 LastName1' ; "$user". The output is string @Name=Firstname1 LastName1.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 at 14:35

























        answered Nov 12 at 14:27









        mklement0

        126k20239267




        126k20239267



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264123%2fremove-certain-characters-from-write-host-output-in-powershell-get-aduser%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto