PowerShell Tip #12 - Create a file with time stamp

by rahul 3/13/2013 2:22:47 PM

While working with PowerShell scripts, quite frequently you may find a need to create a file to store information. The following lines can help you in creating a file name with TimeStamp.

 "MyLogFile-$(get-date -uformat '%Y-%m-%d-%H-%M-%S-%p').log" 

The output is something like...

MyLogFile-2013-03-13-14-07-40-PM.log

To create a new file at a specific $Path, use the following...

 $LogFile = new-item -type file -name "MyLogFile-$(get-date -uformat '%Y-%m-%d-%H-%M-%S-%p').log" -path "$Path" 

$LogFile fill contain the handle and you can easily do an Add-Item $LogFile "Something"

Hope this helps, Wave
Rahul

Quote of the day:
"Most advances in science come when a person for one reason or another is forced to change fields." - Peter Borden



blog comments powered by Disqus

Rahul Soni

Rahul Soni  Twitter

 LinkedIn

 Facebook

 Email me



Vivek Kumbhar

Vivek Kumbhar  Twitter

 LinkedIn

 Facebook

 Email me


Stack Exchange

profile for Vivek at Server Fault, Q&A for system administrators and IT professionals

profile for Rahul Soni at Stack Overflow, Q&A for professional and enthusiast programmers

Calendar

<<  May 2013  >>
MoTuWeThFrSaSu
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

View posts in large calendar

All Items
Sign in

Visit Microsoft's Site

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2013, Rahul Soni

Powered by BlogEngine.NET 1.4.5.0