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
Solved

Getting the time updated in powershell

Posted on 2014-03-28
4
230 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 40

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

839 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