Grzegorz Grzegorz - 1 year ago 131
PowerShell Question

Getting the date taken parameter for the image with a name as variable

I want to get the

Date Taken
parameter for an image (identified by
variable with
constant) and pass it to the main batch script; within the batch script I must use a Powershell instruction (it is not possible to get the
Date Taken
directly by cmd command).

I have to provide a
variable to the Powershell script and at the same time bypass the restricted execution policy.

Briefly, the date taken for the named image should be returned from the Powershell script and set to the
variable within the batch file.

I have the following lines of commands within the Powershell 'DateTaken.ps1' script:


$pic=New-Object System.Drawing.Bitmap $imagePath;
$DATE = $pic.GetPropertyItem(36867).value[0..9];

$strYear = [Char]$date[0],[Char]$date[1],[Char]$date[2],[Char]$date[3];
$strMonth = [Char]$date[5],[Char]$date[6];
$strDay = [Char]$date[8],[Char]$date[9];
$DateTaken = $strYear + "." + $strMonth + "." + $strDay;

and within the batch file:

I am not able to run the set of Powershell instructions listed above in order to get to the
variable and assign it to Data variable within the batch script.

If I get the set of the above Powershell instructions to return the value of
, I will be able to pass it into the temp.txt file using the grater than sign '> temp.txt' and then read it into the batch file by
set /p Data< temp.txt

Answer Source

I think your problem is, that set requires a String and $DateTaken in your script contains an array. The other thing is, that you have to output $DateTaken.

So that's what I did and this script outputs me the date as String.


$imagePath = "C:\temp\pic2.jpg"
$pic=New-Object System.Drawing.Bitmap $imagePath

$picDate = $pic.GetPropertyItem(36867).value[0..9]

$strYear = [String][Char]$picdate[0]+[String][Char]$picdate[1]+[String][Char]$picdate[2]+[String][Char]$picdate[3]
$strMonth = [String][Char]$picdate[5]+[String][Char]$picdate[6]
$strDay = [String][Char]$picdate[8]+[String][Char]$picdate[9]
$DateTaken = $strYear + "." + $strMonth + "." + $strDay

Thanks @MathiasR.Jessen

shorter - more powershellway :)


$imagePath = "C:\temp\pic2.jpg"
$pic=New-Object System.Drawing.Bitmap $imagePath

$picDate = $pic.GetPropertyItem(36867).value[0..9]

$DateTaken = [char[]]$picdate -join '' -replace '\D','.'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download