I Need a Little Help Running Compress-Archive with Read-Host

agfreesafety3 41 Reputation points
2021-03-25T03:02:59.953+00:00

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?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,891 Reputation points Microsoft Vendor
    2021-03-25T06:40:45.757+00:00

    Hi,

    You can store the paths in some variables

    $source = Read-Host -Prompt 'Enter the source path'  
    $destination = Read-Host -Prompt 'Enter the destination path'  
    Compress-Archive -Path $source -DestinationPath $destination  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments