Tag Archives: Powershell

Powershell enable execution of script

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.

Simple Powershell FTP

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.

$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)

Powershell script sort list file

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