Sometimes you might need to download a file with PowerShell from a FTP-server or from a website. For instance, you might need a file from HR that contains new users that should be created in the local Active Directory. In this post I will show you the easiest way to do this and in a later one I will show you a more advanced way, but with more possibilities.
The CmdLet we use in this example is called Invoke-WebRequest. It allows us to both download and upload files from a remote server. It also supports credentials and proxies.
$source = "http://webpage.com/file.xml" $destination = "C:\LocalFolder\newfile.xml" Invoke-WebRequest $source -OutFile $destination
As you can see, we set the complete path to file on the webpage that we want as $Source and specify the path to our local system where we want to save the file. Remember to specify the complete path, including the file name that we want and the extension!
Similar to HTTP, we need to specify the complete path to the file that we want to download and the path to where we want to save the file, including the extension. The main difference here, is that we need to supply the credentials to FTP-Server as well.
$source = "ftp://ftpsite.com/file.xml" $destination = "C:\LocalFolder\newfile.xml" Invoke-WebRequest $source -OutFile $destination -Credential ftpUser
This is the basics on how to download files using PowerShell. In a later guide I will show you how download a file using a function that also prints the progress.