Schedule the task for one day before

Welcome Forums General PowerShell Q&A Schedule the task for one day before

This topic contains 1 reply, has 2 voices, and was last updated by

1 year, 1 month ago.

  • Author
  • #98674

    Topics: 5
    Replies: 3
    Points: 1
    Rank: Member

    Hi, I have made a script which move all monthly report to a specific folder after creating a new folder of the current month in "yyyy-MM" format. For example if it runs on 30th April, it will create a new folder "2018-APR" and then move all files there. The requirement is to run this script at the last day of every month but in our application, we don't have any option to select last day, instead we can select a specific date. The problem is if we select 30th of every month, what will happen in the months with 31 days or feb? So we are planning to run it on 1st of every month but folder name should be of previous month. Means, if we are running the script on 1st of May, it should also create folder "2018-APR". I am currently using following string:

    New-Item -ItemType Directory -Path "$folder1\$(Get-Date -Format yyyy)\$(Get-Date -Format yyyy-MMM)" -Force
    Get-ChildItem -File -Path "$folder2" | Move-Item -Destination "$folder1\$(Get-Date -Format yyyy)\$(Get-Date -Format yyyy-MMM)"

    I have tried "$(get-date).adddays(-1) -format yyyy-MM" string but it's not working. Can someone help?

  • #98677

    Topics: 18
    Replies: 244
    Points: 109
    Rank: Participant

    Have you tried something like this ?

    $ReportDate =(Get-Date).AddDays(-1).ToString("yyyy-MM")
    New-Item -ItemType Directory -Path "$folder1\$reportdate"

The topic ‘Schedule the task for one day before’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort