Wednesday, April 29, 2009

Window PowerShell 2 Quick Tips

Windows PowerShell

Windows PowerShell is a command-line shell and scripting environment that brings the power of the .NET Framework to command-line users and script writers. It introduces a number of powerful new concepts that enables you to extend the knowledge you have gained and the scripts you have created within the Windows Command Prompt and Windows Script Host environments.


Figure 1.0 PowerShell window.

Windows PowerShell V2 Community Technology Preview 3 (CTP3)


PowerShell Benefits
  • Extension to command line windows.
  • Performing common system administration tasks. E.g. managing the registry, services, processes, and event logs, and using Windows Management Instrumentation(WMI).
  • Simplified, command-based navigation of the operating system, which lets users navigate the registry and other data stores
  • Read WMI object values and invoke its method directly.
Useful Commands (Examples):
I hope the following list of examples could get you playing:

1. Get help on particular command: man . E.g. man get-service

2. See a list of command available: get-command

3. Clear the screen: clear-host

4. Show info for Http Web Server service: get-service -include {w3svc} | where-object {$_.status -like "*running*"} | format-list (As in Figure 1.0 command)

5. Get the system date: get-date

6. Get the service DisplayName only: get-service w3svc | format-list -property DisplayName

7. List service : get-service | where-object {$_.name -like "[v-z]*"} | Sort -descending "Status"

8. Show .net component methods:
[System.Math] | Get-Member -MemberType Methods

9. Get newest 5 system event log entry:
Get-EventLog -newest 5 -ComputerName confucius -logname System | Format-List Index, Message
10. List a service member. The following snippet filter the remote service on a machine fireserver name to httpd (Apache Web server)
"httpd"| Get-Service -computername fireserver | Get-Member
11. Start a service remotely
(Get-WmiObject -Computername Win32_Service -Filter "Name='
'").InvokeMethod("StopService",$null)

e.g.
(Get-WmiObject -Computername fireserver Win32_Service -Filter "DisplayName='Computer Browser'").InvokeMethod("StopService",$null)

12.
Copy files
Copy-Item c:\mydoc\text.doc c:\backup

13. Create file/folder
New-Item c:\scripts\Windows PowerShell -type directory

14. Check file exist
Test-Path c:\scripts\
text.doc

15. Write data into xml file
Get-Process | Export-Clixml c:\scripts\process-list.xml

16. Run a program. Use Invoke-Expression
to run a Powershell script.
Invoke-Item notepad.

17. How to see all previous commands and batch it up?
Get-History
or
h // alias for Get-History

Running it in batch
Invoke-History 34;Invoke-History 23

18. Filter command result by its property name
Get-Process -ProcessName svch* // list process with name starts with svch

Get-Process -ProcessName s?c* // list process with name starts with s followed by any character; followed by c and etc.

19. List environment variables
$env:computername
$env:classpath

More to come ....


Acronyms:

* cmdlet, wmi.

No comments: