Sunday, October 17, 2010

Powershell - Get windows service status on a remote computer

Command to get the remote server service status through powershell is listed below

Get-service  -computername "Servername" -displayname "servicename"

Saturday, October 16, 2010

Powershell to get server uptime

I dont remember from which site i got the below script but it is very useful in my day to day admin life

$wmi=Get-WmiObject -class Win32_OperatingSystem -computer "Computername"
$LBTime=$wmi.ConvertToDateTime($wmi.Lastbootuptime)
[TimeSpan]$uptime=New-TimeSpan $LBTime $(get-date)
Write-host $server “Uptime: ” $uptime.days “Days” $uptime.hours “Hours” $uptime.minutes “Minutes” $uptime.seconds “Seconds”

Get-WmiObject -class Win32_OperatingSystem
This will invoke the Operating System wmi object


ConvertToDateTime
Will convert the output to human readable form


[TimeSpan]$uptime=New-TimeSpan $LBTime $(get-date)
This will compare the output time with the current time and store the result to the string as object

Powershell - Search a string inside a text file

Get-Content cmd let will allow reading content from a text file and the following piped cmdlet can be used to search a string inside the text file given as input


(Get-Content "Full path of the text file") | ? {$_ -match 'searchstring''}

Uninstalling Previous Versions of PowerShell

Uninstalling Previous Versions of PowerShell
PowerShell v1 RTM shipped as updates to the OS. Therefore, it will show up only in the updates section. To look for previous versions, do the following:
 On Windows XP/Server 2003:
·         Go to Add/Remove Programs
·         Select Show Updates
·         Look for any of the following KB numbers which indicate earlier versions of PowerShell
o   KB926139 – Windows PowerShell v1.0 RTM – English Language Version
o   KB926140 – Windows PowerShell v1.0 RTM – Localized Installation Package
o   KB926141 – Windows PowerShell v1.0 RTM – MUI pack
o   KB925228 – Windows PowerShell v1.0 RC2
·         Versions of V1 prior to RC2 are MSI based installations and they will appear as Windows PowerShell in the programs list
·         Uninstall the previous version that's installed
On Windows Vista:
·         Go to Programs and Features
·         Click on View Installed Updates
·         Look for any of the following KB numbers which indicate earlier versions of PowerShell
o   KB928439 – Windows PowerShell 1.0 RTM
o   KB923569 – Windows PowerShell 1.0 RC2
·         Version of V1 prior to RC2 are MSI based installations and they will appear as Windows PowerShell in the installed programs list
·         Uninstall the previous version that's installed
On Windows Server 2008:
·         Launch Server Manager
·         Go to Features
·         Click on Uninstall Features. This brings up a list of installed features to choose from
·         Select Windows PowerShell and uninstall
 

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