PowerShell -ExpandProperty and correct date format
up vote
2
down vote
favorite
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
add a comment |
up vote
2
down vote
favorite
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
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
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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
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
powershell select-object
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
add a comment |
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
add a comment |
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")}
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
add a comment |
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
add a comment |
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")}
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
add a comment |
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")}
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
add a comment |
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")}
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
add a comment |
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")}
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")}
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered 2 days ago
mklement0
120k19231259
120k19231259
add a comment |
add a comment |
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")}
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
add a comment |
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")}
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
add a comment |
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")}
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")}
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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