tingsadnewsfer

Subtitle

Blog

Powershell get date x days ago

Posted by [email protected] on


We use a For loop to count down the working days until we get to 1. Wilo This is the code I'm trying to modify... So we need a way to identify files that have not been accessed in a long time.


powershell get date x days ago

Date Time Week will continue tomorrow when I will talk about more cool stuff. Example: Univmsu Univmiu etc each has import ,export and other folders under them. It's more than this solution. This is what I have so far.


powershell get date x days ago

- I just had a request for tips on searching the file system for files matching certain criteria, with file age based on the date created being the blocking issue for this user. Pretty new to PowerShell so any advice would be great.


powershell get date x days ago

Summary : Microsoft Scripting Guy, Ed Wilson, talks about adding and subtracting dates with Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. One of the things I really like about Windows PowerShell is the way it simplifies adding and subtracting from dates. Then I need to get the date of their last log-in, and see if that date is more recent than 120 days ago. 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. I may simply want to see all events from the security log that occurred within a specific date range. 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. Whatever the need, quite often dates come into play. 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. There are six core Windows PowerShell cmdlets that accept DateTime objects as input parameters. Utility Cmdlet Get-EventLog Microsoft. Management Cmdlet Get-Job Microsoft. Core Cmdlet New-TimeSpan Microsoft. Utility Cmdlet Set-Date Microsoft. Utility Cmdlet Test-Path Microsoft. Management 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. AddHours 2 Friday, January 16, 2015 6:27:46 PM I can also use an intermediate variable to hold the DateTime object and to call the method. AddHours 3 Friday, January 16, 2015 7:29:00 PM If I want to add days, it is the same technique, but I call the AddDays method instead of AddHours. AddDays 12 Wednesday, January 28, 2015 4:33:00 PM I can add lots of stuff to DateTime objects. Here is a list of the various Add methods. DateTime Name MemberType Definition ---- ---------- ---------- Add Method datetime Add timespan value AddDays Method datetime AddDays double value AddHours Method datetime AddHours double value AddMilliseconds Method datetime AddMilliseconds double value AddMinutes Method datetime AddMinutes double value AddMonths Method datetime AddMonths int months AddSeconds Method datetime AddSeconds double value AddTicks Method datetime AddTicks long value AddYears Method datetime AddYears int value 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. AddHours -5 Friday, January 16, 2015 11:36:46 AM 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. Some fun with DateTime objects Because 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. I then use the AddMinutes method from the DateTime object that I get from Get-Date, and I display the result. Also, if I am plugged in and charging, it does not display accurate results. That is all there is to adding and subtracting dates. Date Time Week will continue tomorrow when I will talk about more cool stuff. I invite you to follow me on and. If you have any questions, send email to me at , or post your questions on the. Ed Wilson, Microsoft Scripting Guy.

Categories: None

Post a Comment

Oops!

Oops, you forgot something.

Oops!

The words you entered did not match the given text. Please try again.

Already a member? Sign In

0 Comments