Return PowerShell Command result in batch file












0















I have a powershell command that works pretty well in returning the date i want in the format I need. I have been unable to call this command in a batch file to be used in a separate script.



C:Usersxxx>powershell -command (get-date((get-date).addDays(-1)) -uformat "
%Y%m%d")
20171115



This appears to work although the date format isnt what I would like for it to be:



set "psCommand=powershell -command "(get-date((get-date).addDays(-1)))""
for /f "delims=" %%I in ('%psCommand%') do set "leaf=%%I"
echo %leaf% >> uploadsp.txt


Output is: Wednesday, November 15, 2017 5:19:34 PM



Thanks



So I may have gotten ahead oh myself. This is what I had previously and I was merely trying to change it to get date-1.



echo @echo off > uploadsp.txt
set mydate=%date:~10,4%%date:~4,2%%date:~7,2%
echo set mydate=%date:~10,4%%date:~4,2%%date:~7,2% >> uploadsp.txt

set myfile=Epic_DSH360144_Drug_Utilization_%mydate%_DU.txt
echo put %myfile% >> uploadsp.txt
exit









share|improve this question

























  • Confusing question. first line says it returns the format you need. 4th sentence says it works but isn't correct format. Which is correct? Which format do you want? And indeed, current script does work.

    – uSlackr
    Nov 16 '17 at 23:16











  • And you’re not running the same PowerShell command in both examples, The first one includes a -uformat option that is missing from the second.

    – Scott
    Nov 16 '17 at 23:45













  • That is correct, when I include the uformat the output changes from a date to a text depicting the powershell command. I only tried to do powershell to be able to determine date-1 after failing to do it in batch. I have posted my initial code. thanks

    – irshan syed
    Nov 17 '17 at 13:49











  • Let me know if the answer I provided helped resolve or not but likely the issue is with the -format "%%Y%%m%%d as I listed below whether or not you fix the logic you use with that syntax in the batch or use it the way I wrote it which is how I do most of my automated scripts like this.

    – Pimp Juice IT
    Nov 21 '17 at 6:44











  • What's up, any update on the status of your inquiry with respect to the provided answers thus far?

    – Pimp Juice IT
    Dec 10 '17 at 7:03
















0















I have a powershell command that works pretty well in returning the date i want in the format I need. I have been unable to call this command in a batch file to be used in a separate script.



C:Usersxxx>powershell -command (get-date((get-date).addDays(-1)) -uformat "
%Y%m%d")
20171115



This appears to work although the date format isnt what I would like for it to be:



set "psCommand=powershell -command "(get-date((get-date).addDays(-1)))""
for /f "delims=" %%I in ('%psCommand%') do set "leaf=%%I"
echo %leaf% >> uploadsp.txt


Output is: Wednesday, November 15, 2017 5:19:34 PM



Thanks



So I may have gotten ahead oh myself. This is what I had previously and I was merely trying to change it to get date-1.



echo @echo off > uploadsp.txt
set mydate=%date:~10,4%%date:~4,2%%date:~7,2%
echo set mydate=%date:~10,4%%date:~4,2%%date:~7,2% >> uploadsp.txt

set myfile=Epic_DSH360144_Drug_Utilization_%mydate%_DU.txt
echo put %myfile% >> uploadsp.txt
exit









share|improve this question

























  • Confusing question. first line says it returns the format you need. 4th sentence says it works but isn't correct format. Which is correct? Which format do you want? And indeed, current script does work.

    – uSlackr
    Nov 16 '17 at 23:16











  • And you’re not running the same PowerShell command in both examples, The first one includes a -uformat option that is missing from the second.

    – Scott
    Nov 16 '17 at 23:45













  • That is correct, when I include the uformat the output changes from a date to a text depicting the powershell command. I only tried to do powershell to be able to determine date-1 after failing to do it in batch. I have posted my initial code. thanks

    – irshan syed
    Nov 17 '17 at 13:49











  • Let me know if the answer I provided helped resolve or not but likely the issue is with the -format "%%Y%%m%%d as I listed below whether or not you fix the logic you use with that syntax in the batch or use it the way I wrote it which is how I do most of my automated scripts like this.

    – Pimp Juice IT
    Nov 21 '17 at 6:44











  • What's up, any update on the status of your inquiry with respect to the provided answers thus far?

    – Pimp Juice IT
    Dec 10 '17 at 7:03














0












0








0








I have a powershell command that works pretty well in returning the date i want in the format I need. I have been unable to call this command in a batch file to be used in a separate script.



C:Usersxxx>powershell -command (get-date((get-date).addDays(-1)) -uformat "
%Y%m%d")
20171115



This appears to work although the date format isnt what I would like for it to be:



set "psCommand=powershell -command "(get-date((get-date).addDays(-1)))""
for /f "delims=" %%I in ('%psCommand%') do set "leaf=%%I"
echo %leaf% >> uploadsp.txt


Output is: Wednesday, November 15, 2017 5:19:34 PM



Thanks



So I may have gotten ahead oh myself. This is what I had previously and I was merely trying to change it to get date-1.



echo @echo off > uploadsp.txt
set mydate=%date:~10,4%%date:~4,2%%date:~7,2%
echo set mydate=%date:~10,4%%date:~4,2%%date:~7,2% >> uploadsp.txt

set myfile=Epic_DSH360144_Drug_Utilization_%mydate%_DU.txt
echo put %myfile% >> uploadsp.txt
exit









share|improve this question
















I have a powershell command that works pretty well in returning the date i want in the format I need. I have been unable to call this command in a batch file to be used in a separate script.



C:Usersxxx>powershell -command (get-date((get-date).addDays(-1)) -uformat "
%Y%m%d")
20171115



This appears to work although the date format isnt what I would like for it to be:



set "psCommand=powershell -command "(get-date((get-date).addDays(-1)))""
for /f "delims=" %%I in ('%psCommand%') do set "leaf=%%I"
echo %leaf% >> uploadsp.txt


Output is: Wednesday, November 15, 2017 5:19:34 PM



Thanks



So I may have gotten ahead oh myself. This is what I had previously and I was merely trying to change it to get date-1.



echo @echo off > uploadsp.txt
set mydate=%date:~10,4%%date:~4,2%%date:~7,2%
echo set mydate=%date:~10,4%%date:~4,2%%date:~7,2% >> uploadsp.txt

set myfile=Epic_DSH360144_Drug_Utilization_%mydate%_DU.txt
echo put %myfile% >> uploadsp.txt
exit






batch powershell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '17 at 13:47







irshan syed

















asked Nov 16 '17 at 22:22









irshan syedirshan syed

612




612













  • Confusing question. first line says it returns the format you need. 4th sentence says it works but isn't correct format. Which is correct? Which format do you want? And indeed, current script does work.

    – uSlackr
    Nov 16 '17 at 23:16











  • And you’re not running the same PowerShell command in both examples, The first one includes a -uformat option that is missing from the second.

    – Scott
    Nov 16 '17 at 23:45













  • That is correct, when I include the uformat the output changes from a date to a text depicting the powershell command. I only tried to do powershell to be able to determine date-1 after failing to do it in batch. I have posted my initial code. thanks

    – irshan syed
    Nov 17 '17 at 13:49











  • Let me know if the answer I provided helped resolve or not but likely the issue is with the -format "%%Y%%m%%d as I listed below whether or not you fix the logic you use with that syntax in the batch or use it the way I wrote it which is how I do most of my automated scripts like this.

    – Pimp Juice IT
    Nov 21 '17 at 6:44











  • What's up, any update on the status of your inquiry with respect to the provided answers thus far?

    – Pimp Juice IT
    Dec 10 '17 at 7:03



















  • Confusing question. first line says it returns the format you need. 4th sentence says it works but isn't correct format. Which is correct? Which format do you want? And indeed, current script does work.

    – uSlackr
    Nov 16 '17 at 23:16











  • And you’re not running the same PowerShell command in both examples, The first one includes a -uformat option that is missing from the second.

    – Scott
    Nov 16 '17 at 23:45













  • That is correct, when I include the uformat the output changes from a date to a text depicting the powershell command. I only tried to do powershell to be able to determine date-1 after failing to do it in batch. I have posted my initial code. thanks

    – irshan syed
    Nov 17 '17 at 13:49











  • Let me know if the answer I provided helped resolve or not but likely the issue is with the -format "%%Y%%m%%d as I listed below whether or not you fix the logic you use with that syntax in the batch or use it the way I wrote it which is how I do most of my automated scripts like this.

    – Pimp Juice IT
    Nov 21 '17 at 6:44











  • What's up, any update on the status of your inquiry with respect to the provided answers thus far?

    – Pimp Juice IT
    Dec 10 '17 at 7:03

















Confusing question. first line says it returns the format you need. 4th sentence says it works but isn't correct format. Which is correct? Which format do you want? And indeed, current script does work.

– uSlackr
Nov 16 '17 at 23:16





Confusing question. first line says it returns the format you need. 4th sentence says it works but isn't correct format. Which is correct? Which format do you want? And indeed, current script does work.

– uSlackr
Nov 16 '17 at 23:16













And you’re not running the same PowerShell command in both examples, The first one includes a -uformat option that is missing from the second.

– Scott
Nov 16 '17 at 23:45







And you’re not running the same PowerShell command in both examples, The first one includes a -uformat option that is missing from the second.

– Scott
Nov 16 '17 at 23:45















That is correct, when I include the uformat the output changes from a date to a text depicting the powershell command. I only tried to do powershell to be able to determine date-1 after failing to do it in batch. I have posted my initial code. thanks

– irshan syed
Nov 17 '17 at 13:49





That is correct, when I include the uformat the output changes from a date to a text depicting the powershell command. I only tried to do powershell to be able to determine date-1 after failing to do it in batch. I have posted my initial code. thanks

– irshan syed
Nov 17 '17 at 13:49













Let me know if the answer I provided helped resolve or not but likely the issue is with the -format "%%Y%%m%%d as I listed below whether or not you fix the logic you use with that syntax in the batch or use it the way I wrote it which is how I do most of my automated scripts like this.

– Pimp Juice IT
Nov 21 '17 at 6:44





Let me know if the answer I provided helped resolve or not but likely the issue is with the -format "%%Y%%m%%d as I listed below whether or not you fix the logic you use with that syntax in the batch or use it the way I wrote it which is how I do most of my automated scripts like this.

– Pimp Juice IT
Nov 21 '17 at 6:44













What's up, any update on the status of your inquiry with respect to the provided answers thus far?

– Pimp Juice IT
Dec 10 '17 at 7:03





What's up, any update on the status of your inquiry with respect to the provided answers thus far?

– Pimp Juice IT
Dec 10 '17 at 7:03










2 Answers
2






active

oldest

votes


















0














I was able to get this to work as expected as you describe by piping the PowerShell script logic within the batch script to a temp PS1 file, and then using a FOR /F loop to take the result of the executed PowerShell script to subtract the number of days from the current date within it, and save that result as a variable to use in the batch script for your needs (%leaf%) afterwards.



Please note I had to double up the percent signs in the PowerShell -format "%%Y%%m%%d" to ensure those symbols are used as literal characters and not special characters by the batch script.




Escaping Percents



The % character has a special meaning for command line parameters and
FOR parameters.



To treat a percent as a regular character, double it:



%%



Escape Characters, Delimiters and
Quotes






Batch Script



@ECHO ON

:DynamicPSScriptBuild
SET PSScript=%temp%~tmp%~n0.ps1
IF EXIST "%PSScript%" DEL /Q /F "%PSScript%"
ECHO get-date((get-date).addDays(-1)) -uformat "%%Y%%m%%d">>"%PSScript%"

FOR /F "DELIMS=" %%I IN ('Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%'"') DO SET "leaf=%%I"
echo %leaf% >> uploadsp.txt
EXIT




Further Resources




  • FOR /F

  • Escape Characters, Delimiters and Quotes






share|improve this answer































    0














    As per all the help received here, the logic below seemed to work well for my particular needs:



    For /F "delims=" %%G In ('PowerShell -Command "&{((Get-Date).AddDays(-1)).ToString('yyyyMMdd')}"') Do Set "yesterday=%%G"    
    For /F "delims=" %%Y In ('PowerShell -Command "&{((Get-Date).AddDays(-2)).ToString('yyyyMMdd')}"') Do Set "daybefore=%%Y"





    share|improve this answer


























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "3"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1269124%2freturn-powershell-command-result-in-batch-file%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      I was able to get this to work as expected as you describe by piping the PowerShell script logic within the batch script to a temp PS1 file, and then using a FOR /F loop to take the result of the executed PowerShell script to subtract the number of days from the current date within it, and save that result as a variable to use in the batch script for your needs (%leaf%) afterwards.



      Please note I had to double up the percent signs in the PowerShell -format "%%Y%%m%%d" to ensure those symbols are used as literal characters and not special characters by the batch script.




      Escaping Percents



      The % character has a special meaning for command line parameters and
      FOR parameters.



      To treat a percent as a regular character, double it:



      %%



      Escape Characters, Delimiters and
      Quotes






      Batch Script



      @ECHO ON

      :DynamicPSScriptBuild
      SET PSScript=%temp%~tmp%~n0.ps1
      IF EXIST "%PSScript%" DEL /Q /F "%PSScript%"
      ECHO get-date((get-date).addDays(-1)) -uformat "%%Y%%m%%d">>"%PSScript%"

      FOR /F "DELIMS=" %%I IN ('Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%'"') DO SET "leaf=%%I"
      echo %leaf% >> uploadsp.txt
      EXIT




      Further Resources




      • FOR /F

      • Escape Characters, Delimiters and Quotes






      share|improve this answer




























        0














        I was able to get this to work as expected as you describe by piping the PowerShell script logic within the batch script to a temp PS1 file, and then using a FOR /F loop to take the result of the executed PowerShell script to subtract the number of days from the current date within it, and save that result as a variable to use in the batch script for your needs (%leaf%) afterwards.



        Please note I had to double up the percent signs in the PowerShell -format "%%Y%%m%%d" to ensure those symbols are used as literal characters and not special characters by the batch script.




        Escaping Percents



        The % character has a special meaning for command line parameters and
        FOR parameters.



        To treat a percent as a regular character, double it:



        %%



        Escape Characters, Delimiters and
        Quotes






        Batch Script



        @ECHO ON

        :DynamicPSScriptBuild
        SET PSScript=%temp%~tmp%~n0.ps1
        IF EXIST "%PSScript%" DEL /Q /F "%PSScript%"
        ECHO get-date((get-date).addDays(-1)) -uformat "%%Y%%m%%d">>"%PSScript%"

        FOR /F "DELIMS=" %%I IN ('Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%'"') DO SET "leaf=%%I"
        echo %leaf% >> uploadsp.txt
        EXIT




        Further Resources




        • FOR /F

        • Escape Characters, Delimiters and Quotes






        share|improve this answer


























          0












          0








          0







          I was able to get this to work as expected as you describe by piping the PowerShell script logic within the batch script to a temp PS1 file, and then using a FOR /F loop to take the result of the executed PowerShell script to subtract the number of days from the current date within it, and save that result as a variable to use in the batch script for your needs (%leaf%) afterwards.



          Please note I had to double up the percent signs in the PowerShell -format "%%Y%%m%%d" to ensure those symbols are used as literal characters and not special characters by the batch script.




          Escaping Percents



          The % character has a special meaning for command line parameters and
          FOR parameters.



          To treat a percent as a regular character, double it:



          %%



          Escape Characters, Delimiters and
          Quotes






          Batch Script



          @ECHO ON

          :DynamicPSScriptBuild
          SET PSScript=%temp%~tmp%~n0.ps1
          IF EXIST "%PSScript%" DEL /Q /F "%PSScript%"
          ECHO get-date((get-date).addDays(-1)) -uformat "%%Y%%m%%d">>"%PSScript%"

          FOR /F "DELIMS=" %%I IN ('Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%'"') DO SET "leaf=%%I"
          echo %leaf% >> uploadsp.txt
          EXIT




          Further Resources




          • FOR /F

          • Escape Characters, Delimiters and Quotes






          share|improve this answer













          I was able to get this to work as expected as you describe by piping the PowerShell script logic within the batch script to a temp PS1 file, and then using a FOR /F loop to take the result of the executed PowerShell script to subtract the number of days from the current date within it, and save that result as a variable to use in the batch script for your needs (%leaf%) afterwards.



          Please note I had to double up the percent signs in the PowerShell -format "%%Y%%m%%d" to ensure those symbols are used as literal characters and not special characters by the batch script.




          Escaping Percents



          The % character has a special meaning for command line parameters and
          FOR parameters.



          To treat a percent as a regular character, double it:



          %%



          Escape Characters, Delimiters and
          Quotes






          Batch Script



          @ECHO ON

          :DynamicPSScriptBuild
          SET PSScript=%temp%~tmp%~n0.ps1
          IF EXIST "%PSScript%" DEL /Q /F "%PSScript%"
          ECHO get-date((get-date).addDays(-1)) -uformat "%%Y%%m%%d">>"%PSScript%"

          FOR /F "DELIMS=" %%I IN ('Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%'"') DO SET "leaf=%%I"
          echo %leaf% >> uploadsp.txt
          EXIT




          Further Resources




          • FOR /F

          • Escape Characters, Delimiters and Quotes







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 '17 at 15:14









          Pimp Juice ITPimp Juice IT

          25.2k114177




          25.2k114177

























              0














              As per all the help received here, the logic below seemed to work well for my particular needs:



              For /F "delims=" %%G In ('PowerShell -Command "&{((Get-Date).AddDays(-1)).ToString('yyyyMMdd')}"') Do Set "yesterday=%%G"    
              For /F "delims=" %%Y In ('PowerShell -Command "&{((Get-Date).AddDays(-2)).ToString('yyyyMMdd')}"') Do Set "daybefore=%%Y"





              share|improve this answer






























                0














                As per all the help received here, the logic below seemed to work well for my particular needs:



                For /F "delims=" %%G In ('PowerShell -Command "&{((Get-Date).AddDays(-1)).ToString('yyyyMMdd')}"') Do Set "yesterday=%%G"    
                For /F "delims=" %%Y In ('PowerShell -Command "&{((Get-Date).AddDays(-2)).ToString('yyyyMMdd')}"') Do Set "daybefore=%%Y"





                share|improve this answer




























                  0












                  0








                  0







                  As per all the help received here, the logic below seemed to work well for my particular needs:



                  For /F "delims=" %%G In ('PowerShell -Command "&{((Get-Date).AddDays(-1)).ToString('yyyyMMdd')}"') Do Set "yesterday=%%G"    
                  For /F "delims=" %%Y In ('PowerShell -Command "&{((Get-Date).AddDays(-2)).ToString('yyyyMMdd')}"') Do Set "daybefore=%%Y"





                  share|improve this answer















                  As per all the help received here, the logic below seemed to work well for my particular needs:



                  For /F "delims=" %%G In ('PowerShell -Command "&{((Get-Date).AddDays(-1)).ToString('yyyyMMdd')}"') Do Set "yesterday=%%G"    
                  For /F "delims=" %%Y In ('PowerShell -Command "&{((Get-Date).AddDays(-2)).ToString('yyyyMMdd')}"') Do Set "daybefore=%%Y"






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jul 25 '18 at 5:06









                  Pimp Juice IT

                  25.2k114177




                  25.2k114177










                  answered Dec 18 '17 at 20:11









                  irshan syedirshan syed

                  612




                  612






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Super User!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1269124%2freturn-powershell-command-result-in-batch-file%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Сан-Квентин

                      8-я гвардейская общевойсковая армия

                      Алькесар