Showing posts with label Dir. Show all posts
Showing posts with label Dir. Show all posts

Thursday, April 26, 2012

Save PowerShell Console session commands and there output in to text file.

 

Hi,

These days i am very busy in some other projects so that's why this months i post only few articles. Sorry for that.

Does anytime you think to save all the commands and there output in to text file? it like storing all of the PowerShell Consoles session data.

If yes and you don't know how to do it , let me explain it.

it is very simple. we just need to use two commands these are: Start-Transcript , Stop-Transcript

Let see how to use it.

Open you Powershell console , and before running or doing anything else , type Start-Transcript and the path to txt file in which you want to save all the output.

Start-Transcript d:\Log.txt

You can notice in below screenshot it is showing that Transcript started and it the output file is D:\Log.txt.

26-04-2012 18-06-03

Now lets run Few Cmdlets.

Dir

26-04-2012 18-08-37

Get-Process

26-04-2012 18-09-31

one wrong command for testing error

26-04-2012 18-10-02

All done now, type Stop-Transcript, cmdlet to stop recording the session. Transcript is stopped and now lets open the Log.txt file and see if that store all the things which we have done in  our PowerShell console session.

26-04-2012 18-11-26

You can see in below screenshot  that everything which we have done is saved in to a text file. :)

 

26-04-2012 18-14-54

Thanks for viewing my blog.

Aman Dhally

Thursday, February 9, 2012

Accessing Windows Environment Variables using PowerShell.

 

Hi,

Before working on script i always thought that these windows “Environment Variables” are not so important, but as i start working on PowerShell and start working on script i realise that these windows “Environment Variables” are very useful. I uses these windows “Environment Variables” in most of my scripts.

Why these windows “Environment Variables” are important?

For example you are using a script which create a SYSTEM REPORT which contain username, computer name, and Logon server, so where you will find these details ? exactly in windows “Environment Variables.  In another example , i am have a script which sent an email to "someone" but it also send a BCC email to sender, so how to access sender email ID ? i know that in windows “Environment Variables doesn't contain the any email-id, but we can create it using some trick using windows “Environment Variables.

Enough theory, Lets Start!!

How to access all windows “Environment Variables.?

In PowerShell accessing of windows “Environment Variables is very simple. the windows “Environment Variables are mounted as PsDrive in PowerShell. You just need to do run DIR on Env:\, like this  "Dir env:\" .

And this will show you all “Environment Variables which you have on your system.

09-02-2012 12-10-25 

How to access the value of “Environment Variables" using PowerShell?.

Actually there are many ways to access the values of windows "Environment Variables", i mostly used two ways to access these values and the result is same in both ways.

using Get-Item

We can use Get-Item to get the value of desired "Environment Variable".  Run the below command.

Get-Item env:\USERNAME

In the output it showing the us the value of USERNAME environment variable but along with value it also showing the "Name" in output. Let omit it.

09-02-2012 12-17-55

(Get-Item env:\USERNAME).value

Enclose the whole command in bracket after closing the bracket we are choosing to show .value properties. and you can seen below it showing us only the value of USERNAME environment variable .

09-02-2012 12-23-18

Using $ENV:

Actually this is the easy method to access the value of windows environment variables. just use $Env: and after colon provide the environment variable whose value you want to see.

$Env:username

09-02-2012 12-28-28

Simple :) isn't.

Usage {at least for me}

Example 1:

I construct email id using Username environment variable. I know in my "Foo" company every one is having there username@foo.net email id. so i construct email id like this

$email = $env:username + '@foo.net'

09-02-2012 12-37-26 

Example 2:

In some of my script i used this trick, So that i know which user is logged on which laptop and at what time.

Write-host "The user `"$env:username`" logged in to laptop  `"$env:computername`" on $(Get-Date)"  -ForegroundColor Yellow

09-02-2012 12-41-25

Video

Thanks for reading ..

Aman Dhally

messenger_freak_adp1 (30)

Monday, January 9, 2012

Enumerating Registry Keys

 

Enumerating Registry Keys

To enumerate all subkeys in a Registry key, you might be using a line like this:

PS> Dir HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall | Select-Object -expand PSPath

Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\AddressBook

Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Connection Manager

(...)

It will return the PowerShell-style path including the provider name, not just the Registry path. If you used the -Name parameter, on the other hand, you just get the subkey names and no path at all:

PS> Dir HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall -Name

AddressBook

Connection Manager

(...)

The easiest way to get the true Registry path is Resolve-Path:

PS> Resolve-Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*

Path

----

HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\AddressBook

HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Connection Manager

(...)

The resulting paths still use PowerShell drives. To get the native Registry paths, try this:

PS> Resolve-Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object -ExpandProperty ProviderPath

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\AddressBook

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Connection Manager

(...)

Thanks

Tuesday, December 13, 2011

How to List Registry Hives

 

PowerTip of the Day, from PowerShell.com:

Use the provider name instead of a drive name when you need to get a list of all Registry Hives:

Dir Registry::

thanks

aman