Passing variable from Batch to Powershell help?

Joined
Sep 29, 2021
Messages
2
Reaction score
0
I am trying to create a zip file using a batch file in windows but from what I have read I guess this isn't possible natively. So I want to attack this from the next best thing a windows user is likely to have, Powershell. Can I call PS, pass a variable from the batch with commands for PS to create the zip? This needs to take place on the desktop.

This is what I have but am confident its nonsense :confused:.

Code:
Powershell.exe Compress-Archive . %variablename%.zip
 
Joined
Nov 23, 2022
Messages
15
Reaction score
0
You can use $env:variable_name syntax to access curent cmd environment variables from powershell. To get hold of your variable you'd use $env:asset
To try, open cmd, do set "myvar=my value", start powershell, do $env:myvar (this will simply print it, but of course you can use it as any other ps variable)
 
Joined
Jan 8, 2023
Messages
27
Reaction score
2
Yes, it is possible to call PowerShell from a batch file and pass variables to it. Here is an example of how you can use PowerShell to create a zip file from a batch file:
Code:
@echo off

set "source=C:\path\to\source\folder"
set "destination=C:\path\to\destination\folder\archive.zip"

powershell.exe -Command "Compress-Archive -Path '%source%' -DestinationPath '%destination%'"

echo Done!

This batch file sets two variables: source and destination. The source variable specifies the path to the folder that you want to compress, and the destination variable specifies the path where you want to save the zip file.

The batch file then calls PowerShell and passes the Compress-Archive command to it, along with the -Path and -DestinationPath parameters. The -Path parameter specifies the path to the source folder, and the -DestinationPath parameter specifies the path where the zip file should be saved.

I hope this helps!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,352
Latest member
SherriePet

Latest Threads

Top