string with in a sting in an invoke

Hi There,

Still new to powershell, and am having a bit of trouble with invoking powershell from a batch file that contains a string with in a string.

So far, this is what I have: powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -Command "gci 'C:\Users\$env:USERNAME\Desktop\Acrobat Reader *.lnk' -exclude 'Acrobat Reader DC.*' | Remove-Item -WhatIf"

I know that using double quotes "" resolves variables, and single quotes ' sends it explicitly with out expanding variables, but I cant seem to find the correct combination to make it work.

Any help would be much appropriated :)

May 25th, 2015 2:28am

You don't need quotes except on one item,  Start simple.

powershell.exe -Command "gci C:\Users\$env:USERNAME\Desktop\Acrobat Reader\*.lnk -exclude 'Acrobat Reader DC.*' | Remove-Item -WhatIf"

Free Windows Admin Tool Kit Click here and download it now
May 25th, 2015 3:10am

Hi jrv,

Thanks for taking the time to help with this one, here are the results:

Running the follwoing command returns a new line with no text other then the prompt dialogue

powershell.exe -Command "gci C:\Users\$env:USERNAME\Desktop\Acrobat Reader*.lnk -exclude 'Acrobat Reader DC.*' | Remove-Item -WhatIf"

and to error check in case there is a flaw in the command itself:

C:\Users\petwil>powershell.exe -Command "gci 'C:\Users\petwil\Desktop\Acrobat Reader*.lnk' -exclude 'Acrobat Reader DC.*' | Remove-Item -WhatIf"

What if: Performing operation "Remove File" on Target "C:\Users\petwil\Desktop\Acrobat Reader 9 - Copy.lnk".
What if: Performing operation "Remove File" on Target "C:\Users\petwil\Desktop\Acrobat Reader 9.lnk".
What if: Performing operation "Remove File" on Target "C:\Users\petwil\Desktop\Acrobat Reader X.lnk".

And the directory has the following contents:

C:\Users\petwil>dir Desktop
 Volume in drive C is Windows
 Volume Serial Number is BE6D-703B

 Directory of C:\Users\petwil\Desktop

14/05/2015  03:48 PM    <DIR>          .
14/05/2015  03:48 PM    <DIR>          ..
23/04/2015  03:47 PM             2,054 Acrobat Reader 9 - Copy.lnk
23/04/2015  03:47 PM             2,054 Acrobat Reader 9.lnk
23/04/2015  03:47 PM             2,054 Acrobat Reader DC.lnk
23/04/2015  03:47 PM             2,054 Acrobat Reader X.lnk
17/04/2015  12:01 PM    <DIR>          Sametime Fix
17/04/2015  12:01 PM           459,782 Sametime Fix.mp4
08/05/2015  08:16 AM               703 SysCAD92.lnk
               6 File(s)        468,701 bytes
               3 Dir(s)  21,412,667,392 bytes free

Any Ideas?

June 3rd, 2015 2:21am

Don't use a batch file.

It's much simpler to simply run your get-childitem command from a PowerShell prompt.

Free Windows Admin Tool Kit Click here and download it now
June 3rd, 2015 10:06am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics