0
@echo off
Powershell.exe -executionpolicy remotesigned -File C:\Users\aborgetti\Desktop\getdate.ps1

FOR /F "usebackq delims=" %%v IN ('powershell -noprofile -File C:\Users\aborgetti\Desktop\getdate.ps1') DO set "d=%%v"

echo %d%

This is the part that I am confused about...not really sure how to set the variable from powershell to a variable in the .bat file.

 FOR /F "usebackq delims=" %%v IN ('powershell -noprofile -File C:\Users\aborgetti\Desktop\getdate.ps1') DO set "d=%%v"

Any help would be much appreciated.

BTW

The $d variable is being used in a set of initial commands for an FTP program called Bluezone FTP 3.2

getdate.ps1 looks like this:

$a = Get-Date
$b = $a.ToString('MMddyy')
write-host $b
Hituptony
  • 2,740
  • 3
  • 22
  • 44
  • You continue to have the same issue because your PowerShell script doesn't output anything to the console. Either stop assigning it to a variable, or output the variable and don't pipe it to anything. – TheMadTechnician May 12 '14 at 21:05
  • @TheMadTechnician see answer, like this? – Hituptony May 12 '14 at 21:10
  • Nope, Write-Host will try to print it to the screen. You don't want it to go to the screen, you want it to be passed to the batch file. Erase the `write-host` part and have that last line just read `$b` ...or if you really want it to `write-` something use `write-output` – TheMadTechnician May 12 '14 at 21:23
  • haha...awesome. doesn't Frode F first solution not even call a ps script? It just calls powershell to create the date? That's pretty cool considering how much syntax it takes batch to write the same thing. – Hituptony May 12 '14 at 21:36
  • You are correct sir, all his first answer does is runs powershell to execute the command to spit back the current date in the desired format. To be honest he probably could have skipped the -noprofile part, but really to get the date in the format you want that's not that much syntax. Consider what you're really doing in batch to get the same result... you're really running a command prompt, so you would run cmd.exe, but this is assumed since you're running a batch file already, then Date /T, then parse out just the MMddyy from the response. Powershell's way is shorter. – TheMadTechnician May 12 '14 at 21:45

1 Answers1

0

Your powershell script needs to output the value, not store it in a variable. Two different solutions(untested), inspired by @zdan's answer in a differen SO question:

FOR /F "usebackq delims=" %%v IN (`powershell -noprofile "& { (get-date).ToString('MMddyy') }"`) DO set "d=%%v"

Or:

getdate.ps1:

$a = Get-Date
$b = $a.ToString('MMddyy')
$b

Batch:

FOR /F "usebackq delims=" %%v IN (`powershell -noprofile -File "C:\Users\ab\Desktop\getdate.ps1"`) DO set "d=%%v"
Community
  • 1
  • 1
Frode F.
  • 52,376
  • 9
  • 98
  • 114
  • I saw that answer by zdan. but for some reason I can't get it to work. See updated question for code I have. Please. And thanks for answer! – Hituptony May 12 '14 at 20:49
  • the bottom solution does not output anything but this: powershell -noprofile -file C:\Users...\..\.\getdate.ps1 – Hituptony May 12 '14 at 20:51
  • The 2nd solution in my answer works perfectly. Are you sure you tried the latest edit? I fixed some backticks a few minutes after posting the answer. – Frode F. May 13 '14 at 06:02