Solved

Getting the time updated in powershell

Posted on 2014-03-28
4
229 Views
Last Modified: 2014-04-11
I have the need to mark the time in some logs that are created by powershell scripts. I use

$date = get-date

The first time the $date variable is used, the date is correct. Iterations following all have the same date. How can I clear the $date variable so that every time I call it, the current date is used?

clear-variable $date shows error shown:
Clear-Variable : Cannot find a variable with name '03/28/2014 17:19:13'.
0
Comment
Question by:LN41
  • 2
4 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 39963058
Just set date to null at the end of each iteration


#Start of loop
$date = Get-Date


End of loop
$date = $null
0
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 39963075
Instead of assigning the results of Get-Date to a variable, if you're not going to use that variable again, just call Get-Date where you need it.

For example
"start time -- $(Get-Date)" | Add-content file.txt
0
 
LVL 12

Expert Comment

by:Carlo-Giuliani
ID: 39964297
Footech has the right idea.  Just replace all occurrences of $date with (get-date).

But if you want to use Clear-Variable, the correct way to do it is:
Clear-Variable 'Date'

Open in new window

0
 
LVL 29

Expert Comment

by:becraig
ID: 39965177
Both of the guys above are right unless you are trying to do something midstream in your flow which cause you to get an error when calling Get-Date.

Having an idea of the snippet of code you are trying to fix would help.

Since you indicate clear variable gives you and error you can simply go for:
#Start of loop
$date = $null 
$date = Get-Date

Open in new window


#Do stuff
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
A brief introduction to what I consider to be the best editor for PowerShell.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question