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('', 'C:\Desktop\My document\test_batch_file\')"

    I am expecting the following output file.

    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('', ""C:\Desktop\My document\test_batch_file\new_file_$"")"


    • 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('', 'C:\Desktop\My document\test_batch_file\new_file_$')"
    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('', 'C:\Desktop\My document\test_batch_file\new_file_$')"

    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('', ""C:\Desktop\My document\test_batch_file\new_file_$"")"

    Thursday, May 4, 2017 1:40 PM