What Can I Do With Windows PowerShell?
A Task-Based Guide to Windows PowerShell Cmdlets
Cmdlets are the heart-and-soul of Windows PowerShell, Microsoft's new command shell/scripting language. This series provides a task-based introduction to Windows PowerShell cmdlets: rather than focusing on the individual cmdlets themselves, the emphasis is on the tasks you can carry out using those cmdlets. These tasks include everything from reading and writing text files to managing event logs to sorting and filtering data.
Dates and Times
The following articles provide a brief introduction to working with dates and times in Windows PowerShell; these tasks include such things as retrieving the current date and time, and performing date arithmetic. As is so often the case with Windows PowerShell, the code snippets found in these articles can either be included within a Windows PowerShell script or typed directly into the Windows PowerShell console.
Using the Set-Date Cmdlet
Before explaining how to use the Set-Date cmdlet we should note that this is a cmdlet you should use with care. For example, if you’re in an Active Directory environment your workstation times must be closely synchronized with your domain controller times; if they aren’t, you might not be able to log on to the domain. Computer times are often assigned using an automatic time service; in that case, you probably don’t need to (and probably shouldn’t) set dates and times by hand. However, if you do have a need to set dates and times manually then Set-Date provides an easy way to do so.
Changing a Computer’s Date and Time
One way to change the date/time on a computer is to use the -date parameter followed by the new date and time. For example, suppose you want to set the date and time to 8:30 AM on June 1, 2006. Here’s how you do that:
Set-Date -date "6/1/2006 8:30 AM"
Set-Date (Get-Date).AddDays(2)
Set-Date (Get-Date).AddHours(-1)
Set-Date -adjust 1:37:0
As you might expect, the Get-Date cmdlet enables you to retrieve the current date and time. As you might also expect, there are a few other interesting tricks you can do with Get-Date, some of which we’ll show you momentarily.
Listing Date and Time Information
Let’s start with the simplest scenario first. If all you want is the current date and time then simply call Get-Date without any additional parameters:
Get-Date
Wed May 10 10:07:25 2006
Get-Date -displayhint date
Get-Date -displayhint time
$A = Get-Date 5/1/2006
$A = Get-Date "5/1/2006 7:00 AM"
- AddSeconds
- AddMinutes
- AddHours
- AddDays
- AddMonths
- AddYears
(Get-Date).AddMinutes(137)
The New-TimeSpan cmdlet provides a way to do date arithmetic within Windows PowerShell. For example, this command tells you the number of days between today’s date and New Year’s Eve 2006:
Performing Date Arithmetic
New-TimeSpan $(Get-Date) $(Get-Date -month 12 -day 31 -year 2006)
Days : 235 Hours : 0 Minutes : 0 Seconds : 0 Milliseconds : 0 Ticks : 203040000000000 TotalDays : 235 TotalHours : 5640 TotalMinutes : 338400 TotalSeconds : 20304000 TotalMilliseconds : 20304000000
Note. All those who knew that there were 20,304,000,000 milliseconds between May 10, 2006 and December 31, 2006 please raise your hands. |
$(Get-Date)
New-TimeSpan $(Get-Date) $(Get-Date -month 12 -day 31 -year 2006)
New-TimeSpan $(Get-Date) $(Get-Date -month 12 -day 31 -year 2006 -hour 23 -minute 30)
from microsoft technet
No comments:
Post a Comment