Thursday, February 17, 2011

What can I do with windows powershell part1

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
  • Changing a Computer’s Date and Time

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.

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"
Need to set the clock ahead exactly two days? This command uses the Get-Date cmdlet and the AddDays method to advance the clock two days:
Set-Date (Get-Date).AddDays(2)
Other methods that can be used here include AddHours, AddMinutes, and AddSeconds. Need to set the clock back 1 hour due to Daylight Saving Time? Then simply set the clock back -1 (minus 1) hours, like this:
Set-Date (Get-Date).AddHours(-1)
Alternatively, you can use the -adjust parameter to adjust the time using the format hours:minutes:seconds. For example, this command sets the clock ahead 1 hour and 37 minutes:
Set-Date -adjust 1:37:0

  • Listing Date and Time Information

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.
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
In return, you’ll get back something similar to this:
Wed May 10 10:07:25 2006
Ah, but suppose you want only the date, not the date and the time? Then just use the -displayhint parameter and specify date:
Get-Date -displayhint date
Or, if you’d prefer just the time:
Get-Date -displayhint time
You can also use Get-Date to create a date-time object for any date/time. For example, this command creates a variable named $A that maps to 12:00 AM on May 1, 2006:
$A = Get-Date 5/1/2006
What’s that? You need to map $A to 7:00 AM on May 1, 2006? Why not:
$A = Get-Date "5/1/2006 7:00 AM"
Get-Date also includes a number of methods for doing some handy-dandy date arithmetic:
  • AddSeconds
  • AddMinutes
  • AddHours
  • AddDays
  • AddMonths
  • AddYears
Need to know the date/time 137 minutes from now? This command will show you:
(Get-Date).AddMinutes(137)



  • Performing Date Arithmetic

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:
New-TimeSpan $(Get-Date) $(Get-Date -month 12 -day 31 -year 2006)
When this command was run on May 10, 2006 we got back the following:
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.
To use New-TimeSpan you just need to pass it a pair of date-time values. The best way to do that is to use the Get-Date method; that helps ensure that you get a pair of date-time objects that New-TimeSpan can work with. For our first date, we simply use the Get-Date cmdlet without any additional parameters (note that the cmdlet must be enclosed in parentheses):
$(Get-Date)
For our second date we also call Get-Date, but we tacked on the -month, -day, and -year parameters, along with the appropriate values:
New-TimeSpan $(Get-Date) $(Get-Date -month 12 -day 31 -year 2006)
What if you need to know how long it is until a more specific time, such as 11:30 PM on December 31st? As usual, no problem: just include the -hour and the -minute parameters along with the appropriate values (for the hours, use the 24-hour time format). In other words:
New-TimeSpan $(Get-Date) $(Get-Date -month 12 -day 31 -year 2006 -hour 23 -minute 30)



from microsoft technet

No comments:

Post a Comment