Time Check in Powershell

Hi,

I am working on a powershell script which will run every hour (Task Scheduler). The script needs to copy a file and the new file will have a Date and the hour on the file name.

I believe I need to use the Get-Date cmdlet, however I need to only check the hour field.

File 1: C:\Data.txt

File 2: C:\Data_Date_Hr.txt

At the end of the day, I am expecting to see 24 files.

C:\Data_20150623_00.txt

-------------------------------

C:\Data_20150623_23.txt

Any help will be greatly appreciated.

June 23rd, 2015 11:59am

not sure I got it, you want only the hour field of get-date cmdlet result?

If that's the case:

get-date -format HH

Free Windows Admin Tool Kit Click here and download it now
June 23rd, 2015 12:05pm

What does "I need to only check the hour field" mean?

Do you mean this?

June 23rd, 2015 12:08pm

Get-Date -Format yyyyMMdd_HH
Formatting Dates and Times - Custom Formatting


Free Windows Admin Tool Kit Click here and download it now
June 23rd, 2015 12:09pm

Yes, that's correct. The new file name needs to have Date and Hour.
June 23rd, 2015 12:38pm

Hi Bill,

If the job runs at 12:01 AM today 06/23/2015, I need to run the copy command so the file created has date and hour.

This is what I am expecting at the end of the day.

C:\Data_20150623_01.txt   Job runs at 1:00 AM

C:\Data_20150623_02.txt   Job runs at 2:00 AM

--------------------------------------------------------

C:\Data_20150623_23.txt   Job runs at 11:00 PM

Free Windows Admin Tool Kit Click here and download it now
June 23rd, 2015 12:45pm

wouldn't be easier if you posted your code? Read your opening post again, people have to guess to understand what you are trying to achieve. If you are new to powershell try to use pseudocode to explain your needs.
June 23rd, 2015 1:03pm

Copy-Item -Path C:\Data.txt -Destination $('C:\Data_{0:yyyyMMdd_HH}.txt' -f (Get-Date))

Free Windows Admin Tool Kit Click here and download it now
June 23rd, 2015 1:03pm

That worked perfect! Thank you,  I appreciate your help and support.
June 23rd, 2015 4:01pm

Get-Date -Format yyyyMMdd_HH
Formatting Dates and Times - Custom Formatting


Free Windows Admin Tool Kit Click here and download it now
June 23rd, 2015 4:02pm

Copy-Item -Path C:\Data.txt -Destination $('C:\Data_{0:yyyyMMdd_HH}.txt' -f (Get-Date))

June 23rd, 2015 4:56pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics