Showing posts with label powershell. Show all posts
Showing posts with label powershell. Show all posts

Wednesday, May 27, 2009

How to find out window environment values another way

Normally, you would show the System Properties window.



Method 1 - Navigate via MyComputer
Right click on the My computer -> Properties -> Goto Advanced tab -> Click on Environment Variables. Then see Temp = %USERPROFILE%\Local Settings\Temp


Method 2 - msinfo32
Type msinfo32 in the run command.


Method 3 - Dos command
Open command prompt by typing in cmd. type the following:
echo %path%

Method 4 - Powershell tool
Open powershell and type
4.1) $env:path // show envrionmen vairable ("path")
4.2) [Environment]::GetEnvironmentVariable("path")


How to show all environment vairables in powershell
Get-Item $env:* or env:*

Friday, May 22, 2009

Save and restore Powershell history session

Use this command to store powershell session history into xml file
Get-History | Export-Clixml "c:\scripts\my_history.xml"

Use this to restore Powershell history from file.
Import-Clixml "C:\PowershellSession.xml" |Add-History


How to show the list of command history?
h or Get-History

How can i find out the alias for Get-History?
Get-Alias -Definition Get-History



What to do if I have duplicate command in the history?
We wish to have a unique entry per command. Edit the window properties and check 'Discard Old Duplicates'.

Set command window to powershell background

The colour for powershell window background RGB(1,36,86). You can set your command window to the same colour.

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.