header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Changing Date Format
Last Post 19 Nov 2008 12:30 PM by SynJunkie. 4 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
SynJunkieUser is Offline
New Member
New Member
Posts:97
Avatar

--
18 Nov 2008 12:36 PM  

Hi

I'm trying to write something to get a list of users created within a certain amount of days.  I'm using get-qaduser and the get-date cmdlet.  I beleive the problem is the format of the date in the CreationDate field and the format that is returned with the get-date cmdlet.  Looking at the help I see that using the format -g option changes the date to the same format in the field but I'm usure how to then remove days using the adddays method.  If I put the get-date -format g into a variable and pipe it through to get-member I notice that it changes it to a string type rather than the system.datetime type it was as get-date.

is there any way to acheive this?

My script is quite simple and looks like this:

 

$startdate = (get-date -format g).addDays(-7)

Get-qaduser | where { $_.creationdate -ge '$startdate' }

any help or advice would be appreciated.

Thanks

Lee

halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:334
Avatar

--
18 Nov 2008 12:54 PM  
You should do the formatting after the adddays(). Here's a couple of ways.
PS C:\Documents and Settings\hrottenberg> (get-date).gettype().name
DateTime
PS C:\Documents and Settings\hrottenberg> (get-date -f g).gettype().name
String
PS C:\Documents and Settings\hrottenberg> $a = (get-date).adddays(-7)
PS C:\Documents and Settings\hrottenberg> "{0:g}" -f $a
11/11/2008 7:53 AM
PS C:\Documents and Settings\hrottenberg> $a.tostring("g")
11/11/2008 7:53 AM
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
SynJunkieUser is Offline
New Member
New Member
Posts:97
Avatar

--
18 Nov 2008 02:15 PM  
thanks Hal. that gives me the correct date format.
2 things though.

can yuo explian what this is doing please: $dateshort = "{0:g}" -f $a


Also, do you have any idea how I can convert my $dateshort variable to a system.datetime type? I get this error when I run the script?

+ Get-qadUser | where { $_.creationdate -ge <<<< '$dateshort' }
The '-ge' operator failed: Could not compare "20/04/2005 11:40:01" to "$dateshort". Error: "Cannot convert value "$date
short" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."".

My script now looks like this:

$datelong = (get-date).adddays(-7)
$dateshort = "{0:g}" -f $datelong

Get-qadUser | where { $_.creationdate -ge '$dateshort' }
halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:334
Avatar

--
18 Nov 2008 03:18 PM  
-f is the formatting operator. It's a (fairly arcane) holdover from .NET. You can do some neat stuff with it, though. Here are some resources:

- cheat sheet
- howto from PowerShell team
- one from the Scripting Guys

Note that the format strings used in get-date's -format parameter, the {0:g} stuff, and the ToString("g") are all the same.

And most times, the best answer to your question is to rearrange your logic so that you are passing the datetime object around, rather than a text representation of it. But when that's not possible, you can use the [datetime]::parse() static method. I'm not a pro on it so I'm not going to go much further. :)



Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
SynJunkieUser is Offline
New Member
New Member
Posts:97
Avatar

--
19 Nov 2008 12:30 PM  
thats great Hal. Thanks for the explanation and for the links.
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer