PowerShell Script to Download a List of Files

Lately I have been playing around with PowerShell. I decided that I will write scripts in order to perform some simple actions, actions that could be scripted generally are not due to the fact that writing the script takes longer than manually doing it.

So here is the first task I would like to automate: sequentially download each file in a list, the list being provided as a text file.

Download a file using PowerShell

For this part, I simply "stole" the code from somewhere else: http://community.bartdesmet.net/blogs/bart/archive/2006/11/25/PowerShell-_2D00_-How-to-download-a-file_3F00_.aspx, plain simple, I don’t think I’ll be able to do better.

Read a text file in an array

I was expecting this to be a bit more complicated, but it turns out to be piece of cake:

$array = Get-Content TextFile.txt

Not too much trouble.

Putting it all together

So now we need to do make these things work together. First, read the content of the file (given as parameter) in an array, then for each item in the array get the client to download it.

Now, there is one little trick here. The WebClient.DownloadFile method’s second argument is the local file. In this case, we want it to have the same file name as the source file name. Leveraging the .NET framework, System.IO.Path is the way to go.

$list = Get-Content $args
 
$clnt = New-Object System.Net.WebClient
 
foreach($url in $list) 
{ 
 
	#Get the filename 
	$filename = [System.IO.Path]::GetFileName($url) 
 
	#Create the output path 
	$file = [System.IO.Path]::Combine($pwd.Path, $filename) 
 
	Write-Host -NoNewline "Getting ""$url""... "
 
	#Download the file using the WebClient 
	$clnt.DownloadFile($url, $file) 
 
	Write-Host "done." 
}

Two remarks on of the Path class:

The script is not error proof, but it good enough for me now.

Edit: I added some Host-Write lines in order to display something in the shell. When downloading a long list of files, it is nice to see the progress.

PowerShell Prompt Customization

I installed PowerShell a few month ago, but never really used it since then. Last week, I read some articles about it and decided to start using it to see what it was worth. Even if it will probably not change my life as a developer, I can say that so far it has been quite interesting.

I’ve been using the Unix/Linux shell for some years now, and from time to time I miss it under Windows. The cmd.exe prompt is really aging and has countless limitations. It get very hard to do some things that would be really easy with a Unix prompt. Enters PowerShell, a command line utility that leverages the .NET platform. But I digress, my point is not to teach what PowerShell is nor to write an article on how to use it, there are already tons of tutorials online that will fulfil that need better than I can possibly do.

When you install PowerShell, you get some "Windows PowerShell" content added to the start menu, and when you open it you get a nice prompt window that opens up, starting in your home directory (note that the size of the window is bigger, I resized it to fit):

PowerShellDefault

Now let’s say that you use Total Commander or any tool of that kind, or that you install PowerShell Prompt Here tool. Using one of these, you open a PowerShell prompt somewhere. Well, you are back with the default cmd.exe size window, which is too small for any nowadays screen. If you want to a nice window like the one you get when starting PowerShell from the start menu, you’ll have to modify your PowerShell profile.

Modify Your Profile

When PowerShell loads, it reads a profile for that is located at $profile variable value. Simply type $profile in the PowerShell window to see where it is (or should be) located. If it does not exist, you can create it.

So, in order to get a nice window, you have to modify some properties of the  object

$a = (Get-Host).UI.RawUI 
 
$a.ForegroundColor = "Gray" 
$a.BackgroundColor = "Black" 
 
$a.WindowTitle = "Windows PowerShell"</p>
 
$b = $a.BufferSize 
$b.Width = 120 
$b.Height = 3000 
$a.BufferSize = $b
 
$b = $a.WindowSize 
$b.Width = 120 
$b.Height = 50 
$a.WindowSize = $b

It’s pretty straightforward.

Modify

The Prompt

I was not very satisfied with the prompt (showing the PWD, present working directory). In order to modify it, you simply have to define a function called "prompt" that returns a String. I found a nice prompt script there, but was not satisfied with it as it hides the drive. I modified it a bit in order to show the drive’s letter no matter how long the PWD is.

function prompt 
{ 
	$m = 30 # maximum prompt length 
	$d = 3 #size of the drive prompt to keep 
	$str = $pwd.Path 
 
	if ($str.length -ge $m + $d) 
	{ 
		# The prompt will begin with "...", 
		# end with ">", and in between contain 
 		# as many of the path characters as will fit, 
		# reading from the end of the path. 
		$str = $str.substring(0, 3) + "(...)" + $str.substring($str.length - ($m + $d) + 4) 
	} 
 
	"$str > "; 
 
}

Now, this is what I wanted, but using it was not satisfactory for some reason. So I figured out that what I wanted was a prompt showing the command number, and the window displaying the PWD in the title.

function prompt 
{ 
	$str = $pwd.Path 
	$nextId = (h -count 1).Id + 1; 
 
	$Host.Ui.Rawui.WindowTitle = $str + " - Windows PowerShell" 
 
	"PS (" + $nextId + ") > "; 
}

So here it is, the prompt exactly as I wanted it.

← Previous Page