Posts Tagged ‘Powershell’

Powershell enable execution of script

April 4th, 2011

By default powershell prevent the user to run a script file (.ps1), in order to enable the execution of script we have to manually enable it.

First check your powershell execution policy

Get-ExecutionPolicy
Restricted

If it shows “Restricted” then you will have to enable it

We can use "Unrestricted" phrase to enable it

Set-ExecutionPolicy Unrestricted

It will prompt confirmation, if you want it to force put "-f" in the command. The command will look like this

Set-ExecutionPolicy Unrestricted -f

Now you can run your scripts on your Powershell.

What people search:

Simple Powershell FTP

May 3rd, 2010

With Powershell we can do FTP transaction really easy. The best feature from Powershell is we can use any dll library in our script, we can import .Net libraries and etc into our script. The script below is simple FTP to upload file to FTP server. In this script I’m using System.Net.WebClient to handle the file transfer.

1
2
3
4
5
6
7
8
9
10
11
$ftpuser = "user"
$ftppass = "12345"
$ftpserver = "localhost"
$file = "C:\test.txt"
$filenewname = "test.txt"
 
$webclient = New-Object System.Net.WebClient
$ftp = "ftp://"+$ftpuser+":"+$ftppass+"@"+$ftpserver+"/"+$filenewname
$uri = New-Object System.Uri($ftp)
 
$webclient.UploadFile($uri,$file)

What people search:

Powershell script sort list file

May 3rd, 2010

It’s pretty easy to sort an output from ls or Get-ChildItem using powershell, you can sort the output by name, length, and date.

Sort By Name

Get-ChildItem C:\ | sort -property Name

Sort By Size

Get-ChildItem C:\ | sort -property Length

Sort By Date Modified

Get-ChildItem C:\ | sort -property LastWriteTime

You also can reorder the sort output with “-descending”
example :

Get-ChildItem C:\ | sort -property LastWriteTime -descending

What people search: