none
Powershell Script RRS feed

  • Question

  • I would like to set a variable for month and year only instead of the sysdate for the following powershell command

    set CURRENT_DATE=%date:~10,4%%date:~4,2%%date:~7,2%
    echo %CURRENT_DATE%

    powershell -Command "(New-Object Net.WebClient).DownloadFile('http://foo.com', 'C:\Desktop\My document\test_batch_file\new_file_%CURRENT_DATE%.zip')"

    I am expecting the following output file.

    new_file_May_2017.zip

    Please help me regarding this. Thanks in advance.

    • Moved by Bill_Stewart Friday, July 7, 2017 4:26 PM Abandoned
    Thursday, May 4, 2017 12:00 PM

All replies

  • You don't need to use environment variables:

    powershell -Command "$currentdate=[datetime]::Now.ToString('MMM_yyyy');$wc=New-Object Net.Webclient;$wc.DownloadFile('http://foo.com', ""C:\Desktop\My document\test_batch_file\new_file_$currentdate.zip"")"


    \_(ツ)_/




    • Edited by jrv Thursday, May 4, 2017 12:21 PM
    Thursday, May 4, 2017 12:17 PM
  • I am getting the following error. I am running the command in command prompt.

    The string starting:
    At line:1 char:115
    + $currentdate=[datetime]::Now.ToString('MMM_yyyy');$wc=New-Object Net.Webclien
    t;$wc.DownloadFile('http://foo.com', 'C:\Desktop\My document\test_batch_file\new_file_$currentdate.zip')"
    is missing the terminator: "

    Thursday, May 4, 2017 12:49 PM
  • I try this way

    powershell -Command "$currentdate=[datetime]::Now.ToString('MMM_yyyy');$wc=New-Object Net.Webclient;$wc.DownloadFile('http://foo.com', 'C:\Desktop\My document\test_batch_file\new_file_$currentdate.zip')"

    File is downloaded successfully but without showing month and year. 

    Thursday, May 4, 2017 1:03 PM
  • You haven't used the single / double quotation marks as advised by jrv.

    Specifically, compare this bit:

    $wc.DownloadFile('http://foo.com', ""C:\Desktop\My document\test_batch_file\new_file_$currentdate.zip"")"

    Thursday, May 4, 2017 1:40 PM