PowerShell -ExpandProperty and correct date format











up vote
2
down vote

favorite
1












I am attempting to use the -ExpandProperty feature in PowerShell to stop the header appearing in the output and format the date without minutes and seconds. This is just to get the created date for an AD Object:



Get-ADComputer -Server $Server -Identity BlahBlah -Properties Created |
Select-Object -ExpandProperty @{Name="Created";Expression={$_.Created.ToString("yyyy-MM-dd")}}


This does not produce a result, only if I exclude the "-ExpandProperty" part will it produce the right date format BUT includes the header "Created" which I don't want.



Any ideas please?










share|improve this question
























  • Could you share the desired output you want? It is a bit unclear at the moment. To you just want the value from that field and nothing else? I mean - Do you want a list of objects or something else?
    – Mötz
    2 days ago












  • Mi Motz, I just want output from that field that looks like: 2018-07-03 not Created ------- 2018-07-03
    – IanB
    2 days ago












  • An no other objects, details or nothing?
    – Mötz
    2 days ago















up vote
2
down vote

favorite
1












I am attempting to use the -ExpandProperty feature in PowerShell to stop the header appearing in the output and format the date without minutes and seconds. This is just to get the created date for an AD Object:



Get-ADComputer -Server $Server -Identity BlahBlah -Properties Created |
Select-Object -ExpandProperty @{Name="Created";Expression={$_.Created.ToString("yyyy-MM-dd")}}


This does not produce a result, only if I exclude the "-ExpandProperty" part will it produce the right date format BUT includes the header "Created" which I don't want.



Any ideas please?










share|improve this question
























  • Could you share the desired output you want? It is a bit unclear at the moment. To you just want the value from that field and nothing else? I mean - Do you want a list of objects or something else?
    – Mötz
    2 days ago












  • Mi Motz, I just want output from that field that looks like: 2018-07-03 not Created ------- 2018-07-03
    – IanB
    2 days ago












  • An no other objects, details or nothing?
    – Mötz
    2 days ago













up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





I am attempting to use the -ExpandProperty feature in PowerShell to stop the header appearing in the output and format the date without minutes and seconds. This is just to get the created date for an AD Object:



Get-ADComputer -Server $Server -Identity BlahBlah -Properties Created |
Select-Object -ExpandProperty @{Name="Created";Expression={$_.Created.ToString("yyyy-MM-dd")}}


This does not produce a result, only if I exclude the "-ExpandProperty" part will it produce the right date format BUT includes the header "Created" which I don't want.



Any ideas please?










share|improve this question















I am attempting to use the -ExpandProperty feature in PowerShell to stop the header appearing in the output and format the date without minutes and seconds. This is just to get the created date for an AD Object:



Get-ADComputer -Server $Server -Identity BlahBlah -Properties Created |
Select-Object -ExpandProperty @{Name="Created";Expression={$_.Created.ToString("yyyy-MM-dd")}}


This does not produce a result, only if I exclude the "-ExpandProperty" part will it produce the right date format BUT includes the header "Created" which I don't want.



Any ideas please?







powershell select-object






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









mklement0

120k19231259




120k19231259










asked 2 days ago









IanB

725




725












  • Could you share the desired output you want? It is a bit unclear at the moment. To you just want the value from that field and nothing else? I mean - Do you want a list of objects or something else?
    – Mötz
    2 days ago












  • Mi Motz, I just want output from that field that looks like: 2018-07-03 not Created ------- 2018-07-03
    – IanB
    2 days ago












  • An no other objects, details or nothing?
    – Mötz
    2 days ago


















  • Could you share the desired output you want? It is a bit unclear at the moment. To you just want the value from that field and nothing else? I mean - Do you want a list of objects or something else?
    – Mötz
    2 days ago












  • Mi Motz, I just want output from that field that looks like: 2018-07-03 not Created ------- 2018-07-03
    – IanB
    2 days ago












  • An no other objects, details or nothing?
    – Mötz
    2 days ago
















Could you share the desired output you want? It is a bit unclear at the moment. To you just want the value from that field and nothing else? I mean - Do you want a list of objects or something else?
– Mötz
2 days ago






Could you share the desired output you want? It is a bit unclear at the moment. To you just want the value from that field and nothing else? I mean - Do you want a list of objects or something else?
– Mötz
2 days ago














Mi Motz, I just want output from that field that looks like: 2018-07-03 not Created ------- 2018-07-03
– IanB
2 days ago






Mi Motz, I just want output from that field that looks like: 2018-07-03 not Created ------- 2018-07-03
– IanB
2 days ago














An no other objects, details or nothing?
– Mötz
2 days ago




An no other objects, details or nothing?
– Mötz
2 days ago












3 Answers
3






active

oldest

votes

















up vote
1
down vote













In PowerShell there nearly always is more than one solution to a problem-



(Get-ADComputer -Server $Server -Identity BlahBlah -Properties Created | 
Select-Object @{N="Created";E{$_.Created.ToString("yyyy-MM-dd")}} ).Created


or



Get-ADComputer -Server $Server -Identity BlahBlah -Properties Created | 
Select-Object @{N="Created";E{$_.Created.ToString("yyyy-MM-dd")}} |
Select-Object -Expand Created


Parameter names can be shorted as long as they are uniquely identifiable and there are also shortcuts (uppercase letters) so -EA is -ErrorAction



A calculated property does IMO make no sense here as it is the only output, so this shold do also:



Get-ADComputer -Server $Server -Identity BlahBlah -Properties Created | 
ForEach-Object {$_.Created.ToString("yyyy-MM-dd")}





share|improve this answer























  • Thanks LotPings - that last one worked perfectly. I should have realized that using a for-each object made more sense than using a calculated property, as you say.
    – IanB
    2 days ago


















up vote
1
down vote













To complement LotPings' helpful answer, which offers effective solutions:



As for why your code didn't work:



While Select-Object's -Property parameter accepts hashtables that define calculated properties (such as in your code), the -ExpandProperty parameter only accepts a property name, as a string.



Therefore, your hashtable is simply stringified, resulting in string literal System.Collections.Hashtable, causing Select-Object to complain, given that there is no property by that name.



The purpose of -ExpandProperty is to output just a property value rather than a custom object with that property.

You therefore do not need a detour via Select-Object, and can just use the value-outputting script block - { $_.Created.ToString("yyyy-MM-dd") } - directly with ForEach-Object instead, as shown at the bottom of LotPings' answer.





However, there is an obscure feature that you forgo by using ForEach-Object: Select-Object allows combining -ExpandProperty with -Property, in which case the properties specified via -Property are added as NoteProperty members to the value of the property specified via -ExpandProperty:



PS> $val = [pscustomobject] @{ one = 'uno'; two = 2 } |
Select-Object -ExpandProperty one -Property two; $val; $val.two
uno
2


Note how the output string value, 'uno' has a copy of the input object's .two property attached to it.



To emulate that with ForEach requires more work:



PS> $val = [pscustomobject] @{ one = 'uno'; two = 2 } | ForEach-Object {
$_.one + '!' | Add-Member -PassThru two $_.two
}; $val; $val.two
uno!
2





share|improve this answer




























    up vote
    0
    down vote













    I don't have access to an AD at the moment, but this could be what you are after



    Updated



    Get-ADComputer -Server $Server -Identity BlahBlah -Properties Created | Select-Object Created | ForEach-Object {$_.Created.ToString("yyyy-MM-dd")}





    share|improve this answer























    • Hi Motz, thanks, I just tried what you suggested but unfortunately it does not produce a result
      – IanB
      2 days ago










    • Try the updated one - now with ( ) around the Get-ADComputer
      – Mötz
      2 days ago










    • Forgot that the Created property of the object. Current solution works on my lab.
      – Mötz
      2 days ago










    • Thanks Motz! I will try when I log back in later, thanks a lot!
      – IanB
      2 days ago











    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',
    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%2f53238445%2fpowershell-expandproperty-and-correct-date-format%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    In PowerShell there nearly always is more than one solution to a problem-



    (Get-ADComputer -Server $Server -Identity BlahBlah -Properties Created | 
    Select-Object @{N="Created";E{$_.Created.ToString("yyyy-MM-dd")}} ).Created


    or



    Get-ADComputer -Server $Server -Identity BlahBlah -Properties Created | 
    Select-Object @{N="Created";E{$_.Created.ToString("yyyy-MM-dd")}} |
    Select-Object -Expand Created


    Parameter names can be shorted as long as they are uniquely identifiable and there are also shortcuts (uppercase letters) so -EA is -ErrorAction



    A calculated property does IMO make no sense here as it is the only output, so this shold do also:



    Get-ADComputer -Server $Server -Identity BlahBlah -Properties Created | 
    ForEach-Object {$_.Created.ToString("yyyy-MM-dd")}





    share|improve this answer























    • Thanks LotPings - that last one worked perfectly. I should have realized that using a for-each object made more sense than using a calculated property, as you say.
      – IanB
      2 days ago















    up vote
    1
    down vote













    In PowerShell there nearly always is more than one solution to a problem-



    (Get-ADComputer -Server $Server -Identity BlahBlah -Properties Created | 
    Select-Object @{N="Created";E{$_.Created.ToString("yyyy-MM-dd")}} ).Created


    or



    Get-ADComputer -Server $Server -Identity BlahBlah -Properties Created | 
    Select-Object @{N="Created";E{$_.Created.ToString("yyyy-MM-dd")}} |
    Select-Object -Expand Created


    Parameter names can be shorted as long as they are uniquely identifiable and there are also shortcuts (uppercase letters) so -EA is -ErrorAction



    A calculated property does IMO make no sense here as it is the only output, so this shold do also:



    Get-ADComputer -Server $Server -Identity BlahBlah -Properties Created | 
    ForEach-Object {$_.Created.ToString("yyyy-MM-dd")}





    share|improve this answer























    • Thanks LotPings - that last one worked perfectly. I should have realized that using a for-each object made more sense than using a calculated property, as you say.
      – IanB
      2 days ago













    up vote
    1
    down vote










    up vote
    1
    down vote









    In PowerShell there nearly always is more than one solution to a problem-



    (Get-ADComputer -Server $Server -Identity BlahBlah -Properties Created | 
    Select-Object @{N="Created";E{$_.Created.ToString("yyyy-MM-dd")}} ).Created


    or



    Get-ADComputer -Server $Server -Identity BlahBlah -Properties Created | 
    Select-Object @{N="Created";E{$_.Created.ToString("yyyy-MM-dd")}} |
    Select-Object -Expand Created


    Parameter names can be shorted as long as they are uniquely identifiable and there are also shortcuts (uppercase letters) so -EA is -ErrorAction



    A calculated property does IMO make no sense here as it is the only output, so this shold do also:



    Get-ADComputer -Server $Server -Identity BlahBlah -Properties Created | 
    ForEach-Object {$_.Created.ToString("yyyy-MM-dd")}





    share|improve this answer














    In PowerShell there nearly always is more than one solution to a problem-



    (Get-ADComputer -Server $Server -Identity BlahBlah -Properties Created | 
    Select-Object @{N="Created";E{$_.Created.ToString("yyyy-MM-dd")}} ).Created


    or



    Get-ADComputer -Server $Server -Identity BlahBlah -Properties Created | 
    Select-Object @{N="Created";E{$_.Created.ToString("yyyy-MM-dd")}} |
    Select-Object -Expand Created


    Parameter names can be shorted as long as they are uniquely identifiable and there are also shortcuts (uppercase letters) so -EA is -ErrorAction



    A calculated property does IMO make no sense here as it is the only output, so this shold do also:



    Get-ADComputer -Server $Server -Identity BlahBlah -Properties Created | 
    ForEach-Object {$_.Created.ToString("yyyy-MM-dd")}






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 2 days ago

























    answered 2 days ago









    LotPings

    15.4k61431




    15.4k61431












    • Thanks LotPings - that last one worked perfectly. I should have realized that using a for-each object made more sense than using a calculated property, as you say.
      – IanB
      2 days ago


















    • Thanks LotPings - that last one worked perfectly. I should have realized that using a for-each object made more sense than using a calculated property, as you say.
      – IanB
      2 days ago
















    Thanks LotPings - that last one worked perfectly. I should have realized that using a for-each object made more sense than using a calculated property, as you say.
    – IanB
    2 days ago




    Thanks LotPings - that last one worked perfectly. I should have realized that using a for-each object made more sense than using a calculated property, as you say.
    – IanB
    2 days ago












    up vote
    1
    down vote













    To complement LotPings' helpful answer, which offers effective solutions:



    As for why your code didn't work:



    While Select-Object's -Property parameter accepts hashtables that define calculated properties (such as in your code), the -ExpandProperty parameter only accepts a property name, as a string.



    Therefore, your hashtable is simply stringified, resulting in string literal System.Collections.Hashtable, causing Select-Object to complain, given that there is no property by that name.



    The purpose of -ExpandProperty is to output just a property value rather than a custom object with that property.

    You therefore do not need a detour via Select-Object, and can just use the value-outputting script block - { $_.Created.ToString("yyyy-MM-dd") } - directly with ForEach-Object instead, as shown at the bottom of LotPings' answer.





    However, there is an obscure feature that you forgo by using ForEach-Object: Select-Object allows combining -ExpandProperty with -Property, in which case the properties specified via -Property are added as NoteProperty members to the value of the property specified via -ExpandProperty:



    PS> $val = [pscustomobject] @{ one = 'uno'; two = 2 } |
    Select-Object -ExpandProperty one -Property two; $val; $val.two
    uno
    2


    Note how the output string value, 'uno' has a copy of the input object's .two property attached to it.



    To emulate that with ForEach requires more work:



    PS> $val = [pscustomobject] @{ one = 'uno'; two = 2 } | ForEach-Object {
    $_.one + '!' | Add-Member -PassThru two $_.two
    }; $val; $val.two
    uno!
    2





    share|improve this answer

























      up vote
      1
      down vote













      To complement LotPings' helpful answer, which offers effective solutions:



      As for why your code didn't work:



      While Select-Object's -Property parameter accepts hashtables that define calculated properties (such as in your code), the -ExpandProperty parameter only accepts a property name, as a string.



      Therefore, your hashtable is simply stringified, resulting in string literal System.Collections.Hashtable, causing Select-Object to complain, given that there is no property by that name.



      The purpose of -ExpandProperty is to output just a property value rather than a custom object with that property.

      You therefore do not need a detour via Select-Object, and can just use the value-outputting script block - { $_.Created.ToString("yyyy-MM-dd") } - directly with ForEach-Object instead, as shown at the bottom of LotPings' answer.





      However, there is an obscure feature that you forgo by using ForEach-Object: Select-Object allows combining -ExpandProperty with -Property, in which case the properties specified via -Property are added as NoteProperty members to the value of the property specified via -ExpandProperty:



      PS> $val = [pscustomobject] @{ one = 'uno'; two = 2 } |
      Select-Object -ExpandProperty one -Property two; $val; $val.two
      uno
      2


      Note how the output string value, 'uno' has a copy of the input object's .two property attached to it.



      To emulate that with ForEach requires more work:



      PS> $val = [pscustomobject] @{ one = 'uno'; two = 2 } | ForEach-Object {
      $_.one + '!' | Add-Member -PassThru two $_.two
      }; $val; $val.two
      uno!
      2





      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        To complement LotPings' helpful answer, which offers effective solutions:



        As for why your code didn't work:



        While Select-Object's -Property parameter accepts hashtables that define calculated properties (such as in your code), the -ExpandProperty parameter only accepts a property name, as a string.



        Therefore, your hashtable is simply stringified, resulting in string literal System.Collections.Hashtable, causing Select-Object to complain, given that there is no property by that name.



        The purpose of -ExpandProperty is to output just a property value rather than a custom object with that property.

        You therefore do not need a detour via Select-Object, and can just use the value-outputting script block - { $_.Created.ToString("yyyy-MM-dd") } - directly with ForEach-Object instead, as shown at the bottom of LotPings' answer.





        However, there is an obscure feature that you forgo by using ForEach-Object: Select-Object allows combining -ExpandProperty with -Property, in which case the properties specified via -Property are added as NoteProperty members to the value of the property specified via -ExpandProperty:



        PS> $val = [pscustomobject] @{ one = 'uno'; two = 2 } |
        Select-Object -ExpandProperty one -Property two; $val; $val.two
        uno
        2


        Note how the output string value, 'uno' has a copy of the input object's .two property attached to it.



        To emulate that with ForEach requires more work:



        PS> $val = [pscustomobject] @{ one = 'uno'; two = 2 } | ForEach-Object {
        $_.one + '!' | Add-Member -PassThru two $_.two
        }; $val; $val.two
        uno!
        2





        share|improve this answer












        To complement LotPings' helpful answer, which offers effective solutions:



        As for why your code didn't work:



        While Select-Object's -Property parameter accepts hashtables that define calculated properties (such as in your code), the -ExpandProperty parameter only accepts a property name, as a string.



        Therefore, your hashtable is simply stringified, resulting in string literal System.Collections.Hashtable, causing Select-Object to complain, given that there is no property by that name.



        The purpose of -ExpandProperty is to output just a property value rather than a custom object with that property.

        You therefore do not need a detour via Select-Object, and can just use the value-outputting script block - { $_.Created.ToString("yyyy-MM-dd") } - directly with ForEach-Object instead, as shown at the bottom of LotPings' answer.





        However, there is an obscure feature that you forgo by using ForEach-Object: Select-Object allows combining -ExpandProperty with -Property, in which case the properties specified via -Property are added as NoteProperty members to the value of the property specified via -ExpandProperty:



        PS> $val = [pscustomobject] @{ one = 'uno'; two = 2 } |
        Select-Object -ExpandProperty one -Property two; $val; $val.two
        uno
        2


        Note how the output string value, 'uno' has a copy of the input object's .two property attached to it.



        To emulate that with ForEach requires more work:



        PS> $val = [pscustomobject] @{ one = 'uno'; two = 2 } | ForEach-Object {
        $_.one + '!' | Add-Member -PassThru two $_.two
        }; $val; $val.two
        uno!
        2






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 days ago









        mklement0

        120k19231259




        120k19231259






















            up vote
            0
            down vote













            I don't have access to an AD at the moment, but this could be what you are after



            Updated



            Get-ADComputer -Server $Server -Identity BlahBlah -Properties Created | Select-Object Created | ForEach-Object {$_.Created.ToString("yyyy-MM-dd")}





            share|improve this answer























            • Hi Motz, thanks, I just tried what you suggested but unfortunately it does not produce a result
              – IanB
              2 days ago










            • Try the updated one - now with ( ) around the Get-ADComputer
              – Mötz
              2 days ago










            • Forgot that the Created property of the object. Current solution works on my lab.
              – Mötz
              2 days ago










            • Thanks Motz! I will try when I log back in later, thanks a lot!
              – IanB
              2 days ago















            up vote
            0
            down vote













            I don't have access to an AD at the moment, but this could be what you are after



            Updated



            Get-ADComputer -Server $Server -Identity BlahBlah -Properties Created | Select-Object Created | ForEach-Object {$_.Created.ToString("yyyy-MM-dd")}





            share|improve this answer























            • Hi Motz, thanks, I just tried what you suggested but unfortunately it does not produce a result
              – IanB
              2 days ago










            • Try the updated one - now with ( ) around the Get-ADComputer
              – Mötz
              2 days ago










            • Forgot that the Created property of the object. Current solution works on my lab.
              – Mötz
              2 days ago










            • Thanks Motz! I will try when I log back in later, thanks a lot!
              – IanB
              2 days ago













            up vote
            0
            down vote










            up vote
            0
            down vote









            I don't have access to an AD at the moment, but this could be what you are after



            Updated



            Get-ADComputer -Server $Server -Identity BlahBlah -Properties Created | Select-Object Created | ForEach-Object {$_.Created.ToString("yyyy-MM-dd")}





            share|improve this answer














            I don't have access to an AD at the moment, but this could be what you are after



            Updated



            Get-ADComputer -Server $Server -Identity BlahBlah -Properties Created | Select-Object Created | ForEach-Object {$_.Created.ToString("yyyy-MM-dd")}






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 2 days ago

























            answered 2 days ago









            Mötz

            66259




            66259












            • Hi Motz, thanks, I just tried what you suggested but unfortunately it does not produce a result
              – IanB
              2 days ago










            • Try the updated one - now with ( ) around the Get-ADComputer
              – Mötz
              2 days ago










            • Forgot that the Created property of the object. Current solution works on my lab.
              – Mötz
              2 days ago










            • Thanks Motz! I will try when I log back in later, thanks a lot!
              – IanB
              2 days ago


















            • Hi Motz, thanks, I just tried what you suggested but unfortunately it does not produce a result
              – IanB
              2 days ago










            • Try the updated one - now with ( ) around the Get-ADComputer
              – Mötz
              2 days ago










            • Forgot that the Created property of the object. Current solution works on my lab.
              – Mötz
              2 days ago










            • Thanks Motz! I will try when I log back in later, thanks a lot!
              – IanB
              2 days ago
















            Hi Motz, thanks, I just tried what you suggested but unfortunately it does not produce a result
            – IanB
            2 days ago




            Hi Motz, thanks, I just tried what you suggested but unfortunately it does not produce a result
            – IanB
            2 days ago












            Try the updated one - now with ( ) around the Get-ADComputer
            – Mötz
            2 days ago




            Try the updated one - now with ( ) around the Get-ADComputer
            – Mötz
            2 days ago












            Forgot that the Created property of the object. Current solution works on my lab.
            – Mötz
            2 days ago




            Forgot that the Created property of the object. Current solution works on my lab.
            – Mötz
            2 days ago












            Thanks Motz! I will try when I log back in later, thanks a lot!
            – IanB
            2 days ago




            Thanks Motz! I will try when I log back in later, thanks a lot!
            – IanB
            2 days ago


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238445%2fpowershell-expandproperty-and-correct-date-format%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Full-time equivalent

            さくらももこ

            13 indicted, 8 arrested in Calif. drug cartel investigation