# Script for downloading files

**URL:** <https://community.simple-help.com/t/script-for-downloading-files/1008>\
**Category:** Alerts and Toolboxes\
**Created:** [March 18, 2022, 3:27pm UTC](https://community.simple-help.com/t/script-for-downloading-files/1008 "2022-03-18T15:27:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Darrell\_Swafford](https://community.simple-help.com/user_avatar/community.simple-help.com/darrell_swafford/32/82_2.png) [@Darrell\_Swafford](https://community.simple-help.com/u/Darrell_Swafford)\
**Post date:** [March 18, 2022, 3:27pm UTC](https://community.simple-help.com/t/script-for-downloading-files/1008/1 "2022-03-18T15:27:47Z")

</div>

I keep running into areas with crappy internet and I need to prep a client computer without disturbing them. So I made a script to download a file from a URL. Example: The client is busy. I cant take over the machine. I can’t download the file to my computer and then transfer it because of crappy internet. I need to download the large file directly to the client’s machine from a service provider, normally done via a browser.

The script asks the tech for the URL of the file then downloads it to the client machine under c:\temp\downloads.

Hopefully, this will help someone else as well. Maybe you can even improve it with a download bar or time remaining etc…hint hint. 🙂

> ##Refrence material:  
> ##[Split-Path (Microsoft.PowerShell.Management) - PowerShell | Microsoft Learn](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/split-path?view=powershell-7.2)  
> ##[https://lazyadmin.nl/powershell/download-file-powershell/](https://lazyadmin.nl/powershell/download-file-powershell/)  
> ##[PowerShell - Create Directory If Not Exists - ShellGeek](https://shellgeek.com/powershell-create-directory-if-not-exists/)  
> ##[https://4sysops.com/archives/use-powershell-to-download-a-file-with-http-https-and-ftp/](https://4sysops.com/archives/use-powershell-to-download-a-file-with-http-https-and-ftp/)
> 
> #Set destination to download file too.  
> $destination = "c:\temp\downloads"
> 
> #Ask tech where to download from.  
> $source = “ServerUtilsAskTech (URL,text,[http://test](http://test))”
> 
> #Check if destination exists. If not create it.  
> if (Test-Path $destination) {  
> Write-Host “Folder Exists”  
> }  
> else  
> {  
> #PowerShell Create directory if not exists  
> New-Item $destination -ItemType Directory  
> Write-Host “Folder Created successfully”  
> }
> 
> #get file name from download url  
> $destination = $destination + $(Split-Path -Path $source -Leaf)
> 
> #Invoke-WebRequest -Uri $source -OutFile $destination
> 
> Import-Module BitsTransfer  
> $Job = Start-BitsTransfer -Source $source -Destination $destination -Asynchronous -Priority normal -TransferType Download  
> echo “Running Job”
> 
> while (($Job.JobState -eq “Transferring”) -or ($Job.JobState -eq “Connecting”)) `  
> { sleep 5;} # Poll for status, sleep for 5 seconds, or perform an action.
> 
> echo “completing job”  
> echo $Job
> 
> Switch($Job.JobState)  
> {  
> “Transferred” {Complete-BitsTransfer -BitsJob $Job}  
> “Error” {$Job | Format-List } # List the errors.  
> }

---

<div class="post-metadata">

**Author:** ![Kevin\_Dillon](https://community.simple-help.com/letter_avatar_proxy/v4/letter/k/c0e974/32.png) [@Kevin\_Dillon](https://community.simple-help.com/u/Kevin_Dillon)\
**Post date:** [March 18, 2022, 6:54pm UTC](https://community.simple-help.com/t/script-for-downloading-files/1008/2 "2022-03-18T18:54:00Z")

</div>

Seems like this would be very convenient indeed. I tried it and got the following error:

At C:\WINDOWS\TEMP\toolbox8036547725440715274run\Download20File-2-1647629459317-eudpw7gh.ps1:33 char:26

- while (($Job.JobState -eq �?oTransferring�??) -or ($Job.JobState -eq …
- 

```
                     ~

```

You must provide a value expression following the ‘-eq’ operator.  
At C:\WINDOWS\TEMP\toolbox8036547725440715274run\Download20File-2-1647629459317-eudpw7gh.ps1:33 char:27

- while (($Job.JobState -eq �?oTransferring�??) -or ($Job.JobState -eq …
- 

```
                      ~~~~~~~~~~~~~~~~~~

```

Unexpected token ‘�?oTransferring�??’ in expression or statement.  
At C:\WINDOWS\TEMP\toolbox8036547725440715274run\Download20File-2-1647629459317-eudpw7gh.ps1:33 char:26

- while (($Job.JobState -eq �?oTransferring�??) -or ($Job.JobState -eq …
- 

```
                     ~

```

Missing closing ‘)’ in expression.  
At C:\WINDOWS\TEMP\toolbox8036547725440715274run\Download20File-2-1647629459317-eudpw7gh.ps1:33 char:26

- while (($Job.JobState -eq �?oTransferring�??) -or ($Job.JobState -eq …
- 

```
                     ~

```

Missing closing ‘)’ after expression in ‘while’ statement.  
At C:\WINDOWS\TEMP\toolbox8036547725440715274run\Download20File-2-1647629459317-eudpw7gh.ps1:33 char:45

- while (($Job.JobState -eq �?oTransferring�??) -or ($Job.JobState -eq …
- 

```
                                        ~

```

Unexpected token ‘)’ in expression or statement.  
At C:\WINDOWS\TEMP\toolbox8036547725440715274run\Download20File-2-1647629459317-eudpw7gh.ps1:33 char:69

- … ($Job.JobState -eq �?oTransferring�??) -or ($Job.JobState -eq �?oConn …
- 

```
                                                             ~

```

You must provide a value expression following the ‘-eq’ operator.  
At C:\WINDOWS\TEMP\toolbox8036547725440715274run\Download20File-2-1647629459317-eudpw7gh.ps1:33 char:70

- … q �?oTransferring�??) -or ($Job.JobState -eq �?oConnecting�??)) { sle …
- 

```
                                             ~~~~~~~~~~~~~~~~

```

Unexpected token ‘�?oConnecting�??’ in expression or statement.  
At C:\WINDOWS\TEMP\toolbox8036547725440715274run\Download20File-2-1647629459317-eudpw7gh.ps1:33 char:69

- … ($Job.JobState -eq �?oTransferring�??) -or ($Job.JobState -eq �?oConn …
- 

```
                                                             ~

```

Missing closing ‘)’ in expression.  
At C:\WINDOWS\TEMP\toolbox8036547725440715274run\Download20File-2-1647629459317-eudpw7gh.ps1:33 char:86

- … �?oTransferring�??) -or ($Job.JobState -eq �?oConnecting�??)) { slee …
- 

```
                                                            ~

```

Unexpected token ‘)’ in expression or statement.  
At C:\WINDOWS\TEMP\toolbox8036547725440715274run\Download20File-2-1647629459317-eudpw7gh.ps1:33 char:87

- … �?oTransferring�??) -or ($Job.JobState -eq �?oConnecting�??)) { sleep …
- 

```
                                                            ~

```

Unexpected token ‘)’ in expression or statement.  
+ CategoryInfo : ParserError: (🙂 [], ParentContainsErrorRecordException  
+ FullyQualifiedErrorId : ExpectedValueExpression

---

<div class="post-metadata">

**Author:** ![Craig\_Harris](https://community.simple-help.com/letter_avatar_proxy/v4/letter/c/ccd318/32.png) [@Craig\_Harris](https://community.simple-help.com/u/Craig_Harris)\
**Post date:** [March 18, 2022, 8:38pm UTC](https://community.simple-help.com/t/script-for-downloading-files/1008/3 "2022-03-18T20:38:50Z")

</div>

I think it’s just “smart curly quotes” instead of “boring straight quotes” issue - does this boring version work?

> **[\##Refrence material:##https://docs.microsoft.com/en-us/powershell/module/micro...](https://pastebin.com/3MNjveAD)**
>
> Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.

---

<div class="post-metadata">

**Author:** ![Kevin\_Dillon](https://community.simple-help.com/letter_avatar_proxy/v4/letter/k/c0e974/32.png) [@Kevin\_Dillon](https://community.simple-help.com/u/Kevin_Dillon)\
**Post date:** [March 18, 2022, 11:29pm UTC](https://community.simple-help.com/t/script-for-downloading-files/1008/4 "2022-03-18T23:29:07Z")

</div>

Yes, this works! I had to add a trailing slash after c:\temp\downloads but its working - this will be very handy. I was always connecting command line and using curl - but nw I will use this. Thank you for sharing!

---

<div class="post-metadata">

**Author:** ![Darrell\_Swafford](https://community.simple-help.com/user_avatar/community.simple-help.com/darrell_swafford/32/82_2.png) [@Darrell\_Swafford](https://community.simple-help.com/u/Darrell_Swafford)\
**Post date:** [March 19, 2022, 12:16am UTC](https://community.simple-help.com/t/script-for-downloading-files/1008/5 "2022-03-19T00:16:50Z")

</div>

ah,ya. Something probably got formated wierd when I copy and pasted it from my toolbox.  
Glad you got it worked out.
