User cmd to download a file

[Pages:9]user cmd to download a file

Adam the Automator.

Download a File with an Alternative PowerShell wget Command.

June Castillote.

Read more posts by this author.

Do you need to download files from the web but hate repeatedly clicking links? If your job involves downloading files from the web regularly, you will probably want to automate the task. Why not use PowerShell to download files much like an alternative PowerShell wget?

Windows PowerShell and PowerShell comes with file-download capabilities. Using PowerShell to download files is a matter of knowing which cmdlets and .NET classes to use and how to use them.

In this article, you'll learn the various ways to use PowerShell to download files from the web.

Table of Contents.

Prerequisites.

Since this is a learning-by-doing article, there are some prerequisites to ensure that you can follow the examples. Below are the basic requirements.

A computer that is running on Windows 10 or higher. This computer is where you will run the scripts/commands featured in this article. Windows PowerShell 5.1 or PowerShell 7.1 (recommended) . Windows 10 already includes Windows PowerShell 5.1. For non-authenticated file downloads, consider using the Tele2 Speedtest site, which is free. If you want to test file downloads with authorization, you may have to build your HTTP file server. An example of a free HTTP file server is HFS by Rejetto .

Using PowerShell to Download Files from URLs: Four Ways.

There are four methods to use PowerShell to download files that do not depend on third-party tools. These are:

Invoke-WebRequest Invoke-RestMethod Start-BitsTransfer .NET WebClient Class .

Whichever one of these four methods you use, the logic and components to make them work are the same. There must be a source URL pointing to the file's location and the destination path to save the downloaded files. If required by the webserver, you need to enter the credentials as well.

The next sections show each of these four methods. In the end, it's up to you to decide which way you would adapt when using PowerShell to download files.

Using Invoke-WebRequest as a PowerShell wget Alternative.

The first method in PowerShell to download files is by using the Invoke-WebRequest cmdlet. Perhaps the most used cmdlet in this article, InvokeWebRequest , can download HTTP, HTTPS, and FTP links.

Whether the source location requires users to log in, the Invoke-WebRequest cmdlet can handle requests with credentials as well.

To download a file, the syntax below shows the minimum parameters required to achieve the desired outcome.

For example, the code below downloads a file with the name 10MB.zip from a website. Then it saves the downloaded file to C:\dload\10MB.zip . You may copy the code below and paste it into your PowerShell session to test.

The demonstration below shows the expected result after running the code above in PowerShell. As you can see, the file download was successful.

How about if the source requires authentication before allowing access? For example, the code below downloads a file from a private website where users must log in.

However, the download failed due to unauthorized access.

If authentication is required, you should add a credential to the request using the -Credential parameter. The first line in the code below prompts you to enter the credential (username and password) and stores it to the $credential variable.

The demonstration below shows what you'd expect to see when you run the above code in PowerShell. As you can see, the Get-Credential cmdlet prompted a PowerShell credential request . This time, using the credential with Invoke-WebRequest resulted in a successful download.

Looking Out for Parsing Errors when using Invoke-WebRequest.

A crucial thing to remember when using Invoke-WebRequest in Windows PowerShell is that, by default, this cmdlet uses the Internet Explorer engine to parse data. The error below may happen when using Invoke-WebRequest on computers without the Internet Explorer in it.

You'll have to re-issue your command, but this time, include the -UseBasicParsing switch.

In Windows PowerShell, you may receive an error message: The response content cannot be parsed because the Internet Explorer engine is not available, or Internet Explorer's first-launch configuration is not complete. Specify the UseBasicParsing parameter and try again.

Starting with PowerShell Core 6.0, the Invoke-WebRequest cmdlet uses basic parsing only. As such, the -UseBasicParsing parameter is no longer necessary.

Using Invoke-RestMethod.

The Invoke-RestMethod cmdlet is more about sending an HTTP or HTTPS request to a RESTful web service. This cmdlet is more suited for requests that interact with REST APIs such as Microsoft Graph API.

When it comes to downloading files straight from the web, Invoke-RestMethod is an excellent contender. Do not be deceived into thinking otherwise. There is not much difference between using Invoke-RestMethod and Invoke-WebRequest when used for downloading files from a direct web link.

Downloading a File using Invoke-RestMethod.

To download a file using Invoke-RestMethod , use the syntax below. You'll notice that the command uses the same parameters as InvokeWebRequest .

In the example code below, the file is downloaded from the URL value in the $source variable. Then, saved to the path defined in the $destination variable.

If the source requires authentication, you can pass the credentials using the -Credential parameter. The example below prompts for the credentials and stores it to the $credential variable. The value of the $credential variable is then passed to the -Credential parameter.

Also, since the file link is an HTTP source and not HTTPS, it means that you are sending an unencrypted authentication. Typically, you should avoid using HTTP sources for security. But if you must use an HTTP source, you need to add the -AllowUnencryptedAuthentication switch to your command.

Using Start-BitsTransfer.

Start-BitsTransfer is designed specifically for transferring files between client and server computers. This PowerShell cmdlet is dependent on the Background Intelligent Transfer Service (BITS) that is native to the Windows operating system.

Because Start-BitsTransfer requires BITS to work means that this cmdlet is not available on non-Windows computers. On the flipside, StartBitsTransfer enjoys the benefits of BITS itself. Some of these benefits are:

Network bandwidth and usage awareness. Interruption handling (resume, auto-resume, pause, etc.) Downloading multiple files as background jobs. Ability to set download job priorities.

Downloading a File.

The fundamental way to use Start-BitsTransfer in PowerShell to download a file is to specify a source and destination. Using the script below, you only need to change the $source and $destination values according to your requirements.

As you can see from the demo below, the file is downloaded to the path c:\dload\100MB.zip .

Suppose the destination is not specified, Start-BitsTransfer downloads and saves the file to the current working directory. For example, if you run Start-BitsTransfer from C:\dload, the file downloads to the same directory.

For downloads that require authentication, Start-BitsTransfer has a -Credential parameter that accepts a PSCredential object.

Downloading Multiple Files.

To demonstrate downloading multiple files, you'll need to create a CSV file with two columns. Name the file filelist.txt . The first column should contain the link to the source, while the second column must contain the destination path. The file contents would like the one below.

Once the CSV file is ready, use the command below to begin the file download. The command imports the CSV file using Import-Csv and passes the contents to Start-BitsTransfer .

Refer to the demo below to see how the code above works. As you can see, the download starts, and you see the download progress. The PowerShell prompt is not available during the download process.

Suppose you want to start the download process as a background job. To do so, you only have to add the -Asynchronous switch at the end of the Start-BitsTransfer command.

Initially, the state of each job would show c onnecting. The screenshot below shows each file download's job id.

Now that you've started the download process, you'll want to check whether the download has been completed. To check the download job

status, use the Get-BitsTransfer cmdlet. As you can see below, the download jobs' status has changed to Transferred .

Using WebClient Class and HttpClient Class (.NET Framework)

PowerShell is based on .NET, and its nature makes it capable of leveraging the power of .NET itself. There's two .NET class you can use in PowerShell to download files; WebClient and HttpClient.

If you want to know more about these two .NET class in more development and technical way, you could start with When to use WebClient vs. HttpClient vs. HttpWebRequest. In the next section, you will learn how to use WebClient and HttpClient in PowerShell to download files from the web.

Downloading a File using .WebClient.

To use the WebClient class, you need to initiate an object as a .WebClient **type. In the example below, the $webClient is the new .WebClient object. Then, using the DownloadFile() method starts the download of the file from the source.

Please copy the code below and run it in your PowerShell session to test. Note that you will not see any progress or output on the screen unless there's an error. However, the PowerShell prompt will be locked until the download is complete.

If the source requires authentication to allow the file download, you can use the code below. The first line prompts for the credential and stores it to the $credentials variable. The value of $credential is then included in the file download request.

According to this Microsoft document: "We don't recommend that you use the WebClient class for new development. Instead, use the .Http.HttpClient class."

It appears that the WebClient class is obsolete, and the new class that Microsoft is endorsing is the HttpClient class. Don't worry, though. The next section talks about using the HttpClient class in PowerShell to download files from the web.

Downloading a File using .Http.HttpClient.

Like the WebClient class, you need to create first the .Http.HttpClient . Using the code below downloads the file from the $source to the $destination . Refer to the comments above each line to know what each line of code does.

The code below is live, and you can test it by running it in your PowerShell session.

In situations where downloading a file requires authentication, you need to add the credential to the HttpClient object. To include a credential to the file download request, create a new .Http.HttpClientHandler object to store the credentials.

You can copy the code below and run it in PowerShell to test. Or you can also run it as a PowerShell script. In this example, the code is saved as download-file.ps1 .

The demo below shows the result when running the PowerShell script to download the file.

At the start, the directory only has the script file in it. There's a prompt to enter the username and password. Then, the script proceeds to download the file. After downloading the file, you can see that the new file is now inside the destination directory.

Conclusion.

Windows PowerShell and PowerShell Core come with built-in capabilities to download files, acting as a PowerShell wget alternative! Whether downloading password-protected sources, single or multiple files ? a PowerShell way is available to you.

The file download methods covered in this article works on both Windows PowerShell and PowerShell Core. This means that these methods apply to both Windows and Non-Windows systems, with the exclusion of Start-BitsTransfer .

And since PowerShell is more than a command prompt, you can translate what you learned into scripts. For you, that would mean an opportunity for automation. No more copying URLs, clicking links, and waiting for downloads manually.

More from Adam The Automator & Friends.

Scan your Active Directory for 750M+ known leaked passwords with a free read-only Specops Password Audit.

We've put together a list of the resources we, at ATA, can wholeheartedly recommend.

Why not write on a platform with an existing audience and share your knowledge with the world?

User cmd to download a file.

I like to know how to create GPO to publish shortcut in a folder on user desktop.

For example, I have a folder on my desktop called "NetworkApp" I want to create a GPO to deploy a "Helpdesk" URL inside the.

Thanks for your help.

All replies.

On your domain controller, open up Group Policy Create a new GP (best practice is to always have separate GPOs for each item rather than 1 GPO that has multiple setting because its easier to turn off a failing GPO) Create a Test OU in Active Directory Make a Test User in Active Directory Make sure that your test user is within that Test OU (or it will not apply) Right-click on your GPO and select edit Expand User "Configuration", "Preferences", and "Windows Settings" Select "Shortcuts" Right-click in the empty space and select "New" and "Shortcut" For "Action" select "Replace" (so that if you want to delete it later it will remove it from everyone's desktop) In the "Name" Field put an appropriate display name for the Web Application shortcut For "Target type" select "File System Object" For "Location" select "Desktop" In the "Target Path" text box put C:\Program Files\Internet Explorer\iexplore.exe or use the browse button to navigate to the location of Internet Explorer ( you don't need quotes for any of these fields ) In the "Arguments" text box put the URL that you need to open with Internet Explorer If you want a dedicated Icon for the shortcut, in the "Icon file path" type the file path of the icon or use the browse button to navigate to the location of the icon (make sure the users have rights to that folder/icon) Click on the Common tab and check "remove this item when it is no longer needed") Click OK and Close all open windows Now go to a computer and login as your test user and you should see the Shortcut with the icon and correct URL.

This also helpful,

Proposed as answer by Vickydad Wang Microsoft contingent staff Friday, December 6, 2019 2:14 AM.

Thank you for posting in our forum.

I agree with Udara Kaushalya's point of view, in order to facilitate your operation, I add some pictures to facilitate your operation

1.In Group Policy Management, create a new group policy object (GPO) in the "Group Policy Objects" folder. 2.Right click this newly created GPO and select "Edit...". 3.Navigate to "User Configuration => Preferences => Windows Settings => Desktop" 4.Right click the "Desktop" object and select "New => Shortcut"

5.Now set all the configuration details of your application shortcut in the next dialog box.

Note: Please be aware of the "Target type" setting. If the shortcut has to be an application shortcut, you have to choose "File System Object". As default it's set to "URL" and thus creates only a shortcut for a website. Therefore if your user wants to open this shortcut, Internet Explorer (or the default browser) opens with a "cannot display this website" message instead of the application.

6.On the "Common" tab check if this group policy preference should run in logged-on user's security context or not.

Note: If you set the ,,Location" to ,,Desktop" then you should make sure on the ,,Common" tab the check box ,,run in logged-on user's security context" is set, because the shortcut will be published on the users own desktop. If you whish to deploy a shortcut to the ,,All Users" profile then you have to set the target to ,,All Users Desktop" and also uncheck the box to run this group policy preference in logged-on user's security context. Usually a normal user doesn't have access to all users profile, but the system account, which runs this group policy preference, has access to it.

7.Now click Apply / OK and close this dialog box. As the last step, back in GPO Management, link the created GPO with the Organizational Unit in which your users reside.

Now your users have only to restart the computer or do a single log-off log-on. So they will receive the newly created desktop shortcut.

Download a file from ftp subdirectories using cmd script.

I was trying to download multiple files from our ftp server using the script:

but it didn't work so I tried to change to back slash:

appeared. What does that mean?

1 Answer 1.

This means that you've told the FTP server you will be transferring by ASCII.

You almost never actually want to do ASCII, it is better to make a habit of using Binary for all transfers. (ASCII breaks the chain of custody, ASCII transfers can modify file contents such as changing New line characters and not sending correct Unicode characters and are completely unsuited to binary file contents, while the speed benefits are trivial.)

You want to make sure your FTP script uses commands separately it looks like you put a CD along with an MGet here.

Example FTP Script:

This would be in a script file which would be specified in the FTP command when you call it from the command line or from a script.

User cmd to download a file.

I'm looking to install Microsoft Teams for All Users,Currently It's installing only for Current user. Is there a way to perform this? Please let me know.

Here's Command Line which I was executing Teams_windows_x64.exe --silent.

All replies.

You can deploy Microsoft Teams using either System Center Configuration Manager (SCCM), Group Policy, or any third-party distribution mechanisms for broad deployment.

Microsoft has provided MSI installation files that admins can use for bulk deployment of Teams to select users or computers.

Hope this is useful!

Best regards, Leon.

From the article linked to above:

"Whichever method you use to deploy Teams, the installer runs in the context of the logged on user, and installs to the %userprofile%\AppData\Local\Microsoft\Teams folder. As you'll see in the comments on this blog post below, this isn't ideal for some environments for a variety of reasons."

That means there is no all-users install method even with the MSI. This is an inherently per-user application. Depending on your circumstances, that may mean it's unusable.

I have found Microsoft user voices where I can see MS is Still working to improve the installation for all users. Is there any updates?

Edited by JiteshKumar Friday, June 1, 2018 3:43 AM.

Is there any update on this?

That means there is no all-users install method even with the MSI. This is an inherently per-user application. Depending on your circumstances, that may mean it's unusable.

Not to mention insecure. Any environment where we disallow execute permission to user-writeable areas won't be able to run Teams. Either that or expose us to zero-day ransomware.

Time to migrate back to Skype for Business.

No wonder MS wants to stop developing Software Restriction Policy.

The Microsoft user voice that you mentioned earlier in this post has been completed.

The Microsoft user voice that you mentioned earlier in this post has been completed.

Indeed, but that does not look at all promising. It links here, which states:

"The Teams MSI will place an installer in Program Files. Whenever a user signs into a new Windows User Profile, the installer will be launched and a copy of the Teams application will be installed in that user's appdata folder "

So, no change to the basic problems which are having a separate copy of the application for each user, and code running from inside the user's profile.

How to Use the wget Linux Command to Download Web Pages and Files.

You can download entire websites using wget and convert the links to point to local sources so that you can view a website offline. The wget utility also retries a download when the connection drops and resumes from where it left off, if possible when the connection returns.

Other features of wget are as follows:

Download files using HTTP, HTTPS, and FTP. Resume downloads. Convert absolute links in downloaded web pages to relative URLs so that websites can be viewed offline. Supports HTTP proxies and cookies. Supports persistent HTTP connections. It can run in the background even when you aren't logged on. Works on Linux and Windows.

How to Download a Website Using wget.

The wget utility downloads web pages, files, and images from the web using the Linux command line. You can use a single wget command to download from a site or set up an input file to download multiple files across multiple sites.

According to the manual page, wget can be used even when the user has logged out of the system. To do this, use the nohup command. For this guide, you will learn how to download this Linux blog: Before you begin, create a folder on your machine using the mkdir command, and then move into the folder using the cd command. mkdir everydaylinuxuser cd everydaylinuxuser wget . The result is a single index.html file that contains the content pulled from Google. The images and stylesheets are held on Google. To download the full site and all the pages, use the following command: wget -r . This downloads the pages recursively up to a maximum of 5 levels deep. Five levels deep might not be enough to get everything from the site. Use the -l switch to set the number of levels you wish to go to, as follows: wget -r -l10 . If you want infinite recursion, use the following: wget -r -l inf . You can also replace the inf with 0 , which means the same thing. There is one more problem. You might get all the pages locally, but the links in the pages point to the original place. It isn't possible to click locally between the links on the pages. To get around this problem, use the -k switch to convert the links on the pages to point to the locally downloaded equivalent, as follows: wget -r -k . If you want to get a complete mirror of a website, use the following switch, which takes away the necessity for using the -r , -k , and -l switches. wget -m . If you have a website, you can make a complete backup using this one simple command. Run wget as a Background Command. You can get wget to run as a background command leaving you able to get on with your work in the terminal window while the files download. Use the following command: wget -b . You can combine switches. To run the wget command in the background while mirroring the site, use the following command: wget -b -m . You can simplify this further, as follows: wget -bm . Logging. If you run the wget command in the background, you don't see any of the normal messages it sends to the screen. To send those messages to a log file so that you can check on progress at any time, use the tail command. To output information from the wget command to a log file, use the following command: wget -o /path/to/mylogfile . The reverse is to require no logging at all and no output to the screen. To omit all output, use the following command: wget -q . Download From Multiple Sites. You can set up an input file to download from many different sites. Open a file using your favorite editor or the cat command and list the sites or links to download from on each line of the file. Save the file, and then run the following wget command: wget -i /path/to/inputfile.

Apart from backing up your website or finding something to download to read offline, it is unlikely that you will want to download an entire website. You are more likely to download a single URL with images or download files such as zip files, ISO files, or image files. With that in mind, you don't have to type the following into the input file as it is time consuming: . If you know the base URL is the same, specify the following in the input file: file1.zip file2.zip file3.zip. You can then provide the base URL as part of the wget command, as follows: wget -B -i /path/to/inputfile. Retry Options. If you set up a queue of files to download in an input file and you leave your computer running to download the files, the input file may become stuck while you're away and retry to download the content. You can specify the number of retries using the following switch: wget -t 10 -i /path/to/inputfile. Use the above command in conjunction with the -T switch to specify a timeout in seconds, as follows: wget -t 10 -T 10 -i /path/to/inputfile. The above command will retry 10 times and connect for 10 seconds for each file link. It is also inconvenient when you download 75% of a 4-gigabyte file on a slow broadband connection, only for the connection to drop. To use wget to retry from where it stopped downloading, use the following command: wget -c file1.zip. If you hammer a server, the host might not like it and might block or kill your requests. You can specify a waiting period to specify how long to wait between each retrieval, as follows: wget -w 60 -i /path/to/inputfile. The above command waits 60 seconds between each download. This is useful if you download many files from a single source. Some web hosts might spot the frequency and block you. You can make the waiting period random to make it look like you aren't using a program, as follows: wget --random-wait -i /path/to/inputfile. Protect Download Limits. Many internet service providers apply download limits for broadband usage, especially for those who live outside of a city. You may want to add a quota so that you don't go over your download limit. You can do that in the following way: wget -q 100m -i /path/to/inputfile. The -q command won't work with a single file. If you download a file that is 2 gigabytes in size, using -q 1000m doesn't stop the file from downloading. The quota is only applied when recursively downloading from a site or when using an input file. Get Through Security. Some sites require you to log in to access the content you wish to download. Use the following switches to specify the username and password. wget --user=yourusername --password=yourpassword. On a multi-user system, when someone runs the ps command, they can see your username and password. Other Download Options. By default, the -r switch recursively downloads the content and creates directories as it goes. To get all the files to download to a single folder, use the following switch: The opposite of this is to force the creation of directories, which can be achieved using the following command:

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download