Friday, September 24, 2010

Thursday, September 23, 2010

Powershell - Find top 10 large files in a disk

Powershell make it easy to list down the files in a drive by size through a single line command. Buying a third party software can be avoided by these simple commands. The command is 

Get-ChildItem c:\ -recurse | sort -property length -desc | select -first 10  | format-table Name, Length -autosize

Get-ChildItem 

This command will list down the members of the input [ here it is C drive]. Recurse option will search all the folder under the drive mentioned

Sort -property length -desc

Sort will sort the results with the input provided by Get-Childitem command, it sorts by Size here and desc will tell the sort command to list items in descending order.

Select -first 10

This command will select the first 10 items, if you remove this command it will display all results.

Format-table Name, Length -autosize

Format-table command will format the results and display it as a table and the parameters will display only Name and Length column of the result. -autosize will tell the command to remove unwanted size and display it within the screen limits.


Windows PowerShell 2.0



Wednesday, September 22, 2010

Powershell - Setting default printer in the Desktop

In Windows 7 setting a default printer through powershell cmdlets is a easier task when compared to VBscript. It can achieved through fewer commands. The commands and their description are given below

CMDLETS

$Printer = Get-WmiObject win32_printer  | where {$_.name -match "XPS"}
$Printer.SetDefaultPrinter()

"XPS" in the command can be replaced by your printer name , unique word which will exactly point your printer can be mentioned within the quotations  

win32_Printer 

This WMI class will hold all the printer information in your computer

SetDefaultPrinter() 

This method inside Win32_Printer will set the selected printer as a default printer



Tuesday, September 21, 2010

Powershell - How to check service packs on computers

Service pack of the operating system can found by calling the wmi object win32_operatingsystem. Following commands will enumerate the service pack 
$SP = gwmi win32_operatingsystem
$SP.serivepackmajoreversion

Monday, September 20, 2010

Install and Configure Windows PowerShell

1. Install the Windows Management Framework
  • Download and install the Windows Management Framework. Choose the package that includes Windows PowerShell v2 and WinRM 2.0, and that applies to your operating system, system architecture, and language.
    Note   If your local computer is protected by a Microsoft Internet Security and Acceleration (ISA) server, see
    Windows PowerShell: FAQs for Administrators.

2. Verify that Windows PowerShell can run scripts

  1. Click Start > All Programs > Accessories > Windows PowerShell.
  2. Do one of the following to open Windows PowerShell:
    • If you're running Windows Vista, Windows 7, or Windows Server 2008 R2, right-click Windows PowerShell and select Run as administrator. If you get a user account control prompt that asks if you would like to continue, respond Continue.
    • If you're running Windows XP or Windows Server 2003, click Windows PowerShell.
  3. Run the following command:
    Get-ExecutionPolicy
  4. If the value returned is anything other than RemoteSigned, you need to change the value to RemoteSigned.
    Note   When you set the script execution policy to RemoteSigned, you can only run scripts that you create on your computer or scripts that are signed by a trusted source.

Enable scripts to run in Windows PowerShell

In Windows PowerShell session you just opened as an administrator, run the following command:
Set-ExecutionPolicy RemoteSigned

Thursday, September 16, 2010

The term is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.

You tried to run a command in the current directory but forgot to prepend it with .\
Write the script in notepad , click save as and type "name.ps1"
In the powershell command prompt type PS > .\name.ps1
or
in Run prompt