Running Compress-Archive with Read-Host

Hello I’m new to Powershell.

I will be running the script from the machine where I’d like the compressed file to be placed, in the path specified. I want to compress the folder from the UNC path (from the first Read-Host), and place a copy of the resulting .zip file into the specified directory on the computer (from the second Read-Host).

I would like some help adding the necessary components to this bit of Powershell code (assuming what I have here works).

I know that I could just run something like:

Compress-Archive \tommc-pc\c$\users\tommc -DestinationPath c:\windows\temp\tommc_windows_home.zip

But I’d like to make it more user friendly, so the user would enter the UNC path for the source path and folder that’s to be compressed, as well as a prompt for the full Destination path and filename of the .zip file on the machine I’m running the script from.

Might you be so kind as to provide some guidance on how I could accomplish this?

The help for Read-Host (Get-Help Read-Host) gives a couple of examples of using variables. At its most basic, that’s what you need to do. Read the prompts into a variable and replace the paths with the variable names:

$source = Read-Host 'Enter the source path'
$destination = Read-Host 'Enter the destination path'
$archive = Read-Host 'Enter the archive name'
Compress-Archive -Path $source -DestinationPath "$destination\$archive"

However, you’re trying to make it more user friendly, which implies you might not trust the users to run the Compress-Archive command correctly on their own. That being the case, you’re probably going to want to add some validation such as did they include the \\ at the start of the UNC path, does the destination folder exist (if not, will you provide a default or create the folder?), and did they remember to add .ZIP to the file name.

To give the opportunity to enter free text is usually more error prone and less user friendly than choosing a file or a folder with the standard dialog boxes from Windows. Search for “PowerSehll open file dialog” or “PowerShell open folder dialog” to find some help about how to do something like this. :wink:

Regardless of that … when you post code, error messages, sample data or console output format it as code, please.

Here you can read how that works: Guide to Posting Code.

Thanks in advance.