Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Getting the time updated in powershell

Posted on 2014-03-28
4
Medium Priority
?
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 41

Accepted Solution

by:
footech earned 2000 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

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

618 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