![]() I use the Get-CimInstance cmdlet to retrieve the Win32_Battery WMI class. ![]() Some fun with DateTime objectsīecause it is so easy to use WMI to find the remaining runtime on a laptop battery, and it is so easy to add minutes, I created a simple script to let me know when my laptop will shut down. The advantage of using the intermediate expressions is it is probably easier to read. ![]() Here is an example of using intermediate expressions:īut I can simplify this into a single line as shown here: I can even combine methods, so I do not have to add -1 day and then add -2 hours by using intermediate expressions (unless I want to). Here, I create a DateTime object that is five hours in the past: The technique works exactly the same way for subtracting hours. So in the following example, I create a DateTime object that is 12 days in the past: If I want to create a DateTime object that represents a date in the past, I still use the appropriate Add method, but I supply a negative number. PS C:> Get-date | Get-Member -MemberType Method -Name a*ĪddDays Method datetime AddDays(double value)ĪddHours Method datetime AddHours(double value)ĪddMilliseconds Method datetime AddMilliseconds(double value)ĪddMinutes Method datetime AddMinutes(double value)ĪddMonths Method datetime AddMonths(int months)ĪddSeconds Method datetime AddSeconds(double value)ĪddTicks Method datetime AddTicks(long value)ĪddYears Method datetime AddYears(int value) Here is a list of the various Add methods. I can add lots of stuff to DateTime objects. This technique is shown here by using the Get-Date cmdlet: If I want to add days, it is the same technique, but I call the AddDays method instead of AddHours. I first create a specific date and time, and then I call the AddHours method: It is also possible to convert a string into a DateTime object by using the type accelerator. I can also use an intermediate variable to hold the DateTime object and to call the method. Here is an example using the Get-Date cmdlet: If I want to add hours to a DateTime object, all I need to do is to create an instance of a DateTime object, and call the AddHours method. Test-Path Adding dates and times-the easy way This technique is shown here for the Test-Path cmdlet: To find which parameters accept a DateTime object, I can use the Get-Help cmdlet and look at the syntax for the various parameter sets. PS C:> Get-Command -ParameterType | ft -AutoSizeĬmdlet Get-Date Ĭmdlet Get-EventLog Ĭmdlet New-TimeSpan Ĭmdlet Set-Date Ĭmdlet Test-Path There are six core Windows PowerShell cmdlets that accept DateTime objects as input parameters. When I know how to create a specific DateTime object, whether in the past, in the future, or even for the present, I can use that DateTime object as an input parameter for other Windows PowerShell cmdlets. Whatever the need, quite often dates come into play. I may want to see how many days until a user’s password expires so that I can warn them when the password comes within 10 days of expiring. I may simply want to see all events from the security log that occurred within a specific date range. I may need to do the same thing with the last time that files were accessed or to examine the date that print jobs were submitted to a print server. Then I need to get the date of their last log-in, and see if that date is more recent than 120 days ago. For example, if I want to find users who haven’t logged in to the domain for 120 days, I need to be able to create a date that was 120 days ago. One of the things I really like about Windows PowerShell is the way it simplifies adding and subtracting from dates. Microsoft Scripting Guy, Ed Wilson, is here. Summary : Microsoft Scripting Guy, Ed Wilson, talks about adding and subtracting dates with Windows PowerShell.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |