PowerShell is quickly becoming the main framework for scripting stuff in the Windows world. By default PowerShell 2.0 is enabled on both Windows 7 and Windows Server 2008 R2.
The PowerShell profile
PowerShell has a profile feature that is a .ps1 script that loads when you open PowerShell. This script is neat for storing custom functions that come in handy. The script is not there by default so you have to create it yourself. You can check if you already have a profile by typing Test-Path $profile To create a profile type New-Item -path $profile -type file -force and notepad $profile to edit it. You need to set executionpolicy to allow scripts or you will get an error. Run PowerShell as Administrator and type Set-ExecutionPolicy RemoteSigned, this allows for local scripts to be exectuted in PowerShell.
Sample script - A PowerShell version of wget
Now that we have our profile what is better to put in it than a Get-File function. This simple script take one or more url's and download them into the destination path. The last command in the script adds the alias wget to Get-File so that you can also type wget To use it simply type something like
Get-File -Url "http://mindre.net/Hyper-V_Monitor.gadget" -DestinationPath "C:\Users\Tore\Desktop"
Put this in your $profile file
function Get-File
{
<#
.SYNOPSIS
A simple PowerShell script to retrive files
.DESCRIPTION
Retrives one or more files to a directory
.EXAMPLE
Get-File -Url "http://mindre.net/Hyper-V_Monitor.gadget" -DestinationPath "C:\Users\Tore\Desktop"
Retrives the file "Hyper-V_Monitor.gadget" to the folder "C:\Users\Tore\Desktop"
.EXAMPLE
Get-File -Url "http://mindre.net/Hyper-V_Monitor.gadget"
Retrives the file "Hyper-V_Monitor.gadget" to the current directory
.NOTES
The script assumes that you'r not behind a web proxy and that the url's end with a filename like "http://my.site.com/logo.jpg".
If the destination path does not exist the script will create it.
.LINK
http://mindre.net/post/PowerShell-profile-and-a-sample-script.aspx
#>
Param
(
[parameter(Mandatory=$true)]
[String[]]
[ValidateNotNullOrEmpty()]
$Url,
[String]
[ValidateNotNullOrEmpty()]
$DestinationPath = (Get-Location)
)
$wClient = New-Object System.Net.WebClient
[System.Net.GlobalProxySelection]::Select = [System.Net.GlobalProxySelection]::GetEmptyWebProxy()
$tp = Test-Path $DestinationPath
if ($tp -eq $false) { New-Item $DestinationPath -ItemType directory -Force | Out-Null }
$Url | % {
$regex = [Regex]::Split($_, "/")
$file = Join-Path $DestinationPath $regex[$regex.Length - 1]
Write-Host $file -NoNewline
$wClient.DownloadFile($_, $file)
Write-Host " 100%"
}
}
if ((Get-Alias -Name wget*) -eq $null) {
New-Item -Path alias:wget -Value Get-File | Out-Null
}
New-Item -Path alias:wget -Value Get-File | Out-Null