Question: I am using `PowerShell "(Get-Date).AddDays(-7).ToString('ddMMyyyy')"` in batch script.

I want to use a variable instead of **Get-Date** function. Is it possible?

`ADate` is the variable name!

**Edited:**

As suggested, my script is:

```
For /F UseBackQ %%A In (
    `PowerShell "(Get-Date).AddDays(-7).ToString('ddMMyyyy')"`
) Do Set "Freq=%%A"

```

`Adate` is simple string which comes from the file name, and has a value like `16112016`.

Answer: 
You need to use a [`for`](https://technet.microsoft.com/en-us/library/cc754900.aspx) loop to get the output of an external command in a batch variable:

```batch
@echo off
for /f "usebackq tokens=*" %%d in (`powershell "..."`) do set "adate=%%d"
echo %adate%

```

The `usebackq` and backticks are just so you don't need to escape the nested single quotes in your command string.
The following takes the last modified time from a known file's properties and creates a variable with a date seven days earlier, *(obviously changing `C:\Test\TestFile.ext` as necessary)*:

```bat
For /F UseBackQ %%A In (
    `PowerShell "((gi 'C:\Test\TestFile.ext').LastWriteTime).AddDays(-7).ToString('ddMMyyyy')"`
) Do Set "ADate=%%A"

```

**Edit**

The following example takes a date string with a known format, *(in this case provided in two variables)*. It then converts that string to a date object, subtracts seven days and sets it back to a string in the new `%ADate%` variable:

```bat
@Echo Off
Set "DateStr=16112016"
Set "DFormat=ddMMyyyy"

For /F UseBackQ %%A In (`Powershell^
 "([datetime]::ParseExact('%DateStr%','%DFormat%', [System.Globalization.CultureInfo]::CurrentCulture)).AddDays(-7).ToString('%DFormat%')"
 `) Do Set "ADate=%%A"
Echo(%ADate%

Timeout -1

```