Working with PowerShell Preference Variables: $OFS (Output field Separator).

 

When you open PowerShell console, type $ and start pressing ‘tab’, you will amaze that there are many pre-built variables exists there.

One of the variable is $OFS, this is a preference variable and full meaning of $OFS is ‘Output Fields Separator’.

What it does?

It specified the character to use as separator while returning the result from the array. By default when the results of an array come, it has a single space as a separator.

By default the value of  $OFS is null.

Let me show to you.

I am using PowerShell version 3.0. , and this $OFS variable is exists in PowerShell version 2 too.

1

When you type and run the  $OFS variable the result is blank.

2

For testing, let’s create a array of the script and checked the output.

[string]$myStringArray = ‘Aman’, ‘Singh’ , ‘Dhally’

3

Now, Lets run the “$myStringArray ” variable.

You can see that, all items in our array result are separated by a single space.

4

Now let’s assign a semi-colon as a separator in $OFS variable.

 $OFS = “;”

4.0

Now create a array of string variable same as before.

Now , you can see, when you run the $myStringArray variable this time, all items of the array are separated with semi-colon rather then like single space before.

6

You can use comma as a separator,

7

or anything you like,  ;)

8

this is very handy, when you are planning to export data in to CSV files, you can have the option to choose separators of your choice  rather then using the single space.

Happy Learning.

Tested on Windows 7 [64 bit]

Regards

Aman Dhally

 
clip_image001 clip_image002 clip_image003 clip_image005  clip_image007

PowerShell Tip : Find Default Printer Installed using Powershell.

The job of System Administrators are very adventurous, sometime you have to find a little-little settings and sometime you have to write a big-big,  hundred’s of line’s scripts.

Today, my task was to find a default printer installed on laptops. To solve it , i used the Win32_Printer class.

You can run the below query on your laptop and this will show you the default installed  printer on your system.

To run the below command on the remote system you can –Computer parameter and make sure you are proper admin rights to access the system.

We are using Get-WmiObject cmdlet and then we are running a WQL query to select everything from Win32_Printer class and choose to show that result where default value is true.

Simple

Get-WmiObject -Query ” SELECT * FROM Win32_Printer WHERE Default=$true”

21-10-2013 17-53-29

If you want to see all printers installed on your laptop, you can run the below code and this will show you the list of all printers,  with their Port Name and location.

Get-WmiObject -Query ” SELECT * FROM Win32_Printer” | Select Name, Default, PortName,Location

21-10-2013 13-53-47

I love WMI .

Happy learning . ;o) .

Tested on : Windows7 [64 bit]

Thanks
Aman Dhally
clip_image001 clip_image002 clip_image003 clip_image005  clip_image007

Powershell Tips : Use Only Powershell V2 cmdlets , with PowershellV3 Installed.

I use Windows 7 (64 bit) with Powershell V3 installed on it. Most of our users are still on PowerShell v2, Sometime when I am writing scripts for them i use some PowerShell v3 cmdlets accidently.

Normally I write the initially version of all scripts on PowerShell console and I do test my all script in the console only.

So I thought there must be a way to run PowerShell.exe with Version 2 only.

I found the solution and i thought I should share it with You.

It’s simple, Just run “Powershell.exe –Version 2” and this will open a PowerShell console with V2 cmdlets only.

Let’s test it.

When i run $PSVersionTable  in my PowerShell console, you can see that  my PSversion is 3.0.

14-10-2013 17-07-33

Now lets run “Powershell.exe –Version 2” .

14-10-2013 16-46-57

and new PowerShell console will open and let’s check it’s $PSVersionTable.

You can see that this PowerShell version of this console is 2.0.

14-10-2013 17-07-59

Now let’s check one more thing.  Let’s see if version 2 is having Version 3.0 cmdlets or not (just to make our self bit  more confident).

Let’s search for all cmdlets those has the word Update. The Version 3 has a new cmdlet named “Update-Help”.

Get-Command *update* -CommandType cmdlet

14-10-2013 17-08-23

and then run the same command on v2 PowerShell console.

You can check that , Update-Help cmdlet is not available on V2 console :)

14-10-2013 17-08-55

Now i can use it and create a V2 script more confidentiality, without worrying of using v3 cmdlets accidentally. . :) 

Note: I tested this on Windows 7 (64 bit) with PowerShell v3 installed.

Thanks for reading and your time,.

Thanks

Aman Dhally

clip_image001 clip_image002 clip_image003 clip_image005  clip_image007

Find Operating System Start time using Powershell.

Today, I have been asked  to do a strange task, My manager told me to find a Operating System Start time.

I was thinking about an half hour  on how to get this done, then i thought about Event Logs, every thing is recorded there, after a few minutes of hardworking , i have found the event which tells us about the Start time of the Operating System.

He wants a history of  start up time of all dates, and luckily windows keep history of start up time in Event Logs.

The Event ID : 12, shows a information on at what time the Operating system was  started.

11-10-2013 17-23-24

and we know how to use Powershell and  how to use Get-EventLog cmdlet.

and then it a matter of few seconds , and I have the list of Operating System’s start time.

run the below command to get the list of start time of the operating system.

Get-EventLog -Log System  |  Where {  $_.EventID -eq 12    } | SelectTimeGenerated

 

Here is the result.

11-10-2013 20-15-13

Tested on : Windows7 (64 Bit)

Happy Weekend. Have fun :) .

Thanks

Aman Dhally

clip_image001 clip_image002 clip_image003 clip_image005  clip_image007

Getting Last Installation Success Date and Last Search Success date of Windows Updates using Powershell.

 

Everyday is a new day , with new problems and new issues, sometime at users side and sometime at Server side. The life in IT is quite happening.

These new issues and problems leads to new solutions and new scripts.

In my environment I asked few users to run their windows updates, but as you know user never listen ;o), So i thought to find out which users had install and run windows update and which is not.

The first thing which come in my mind is to check any com object available to windows update.

Luckily i found a “Microsoft.Update.AutoUpdate” com object and i thought lets give it a try.

I create a new Powershell Object using New-Object cmdlet.

$windowsUpdateObject = New-Object -ComObject Microsoft.Update.AutoUpdate

05-10-2013 00-22-27

My this new variable has a property of Results. So when I access the .Result property using dot notation, i have found the result what i was looking for,  The windows last Search success date and Windows update last installation date.

$windowsUpdateObject.Results

05-10-2013 00-58-11

 

$windowsUpdateObject.Results.LastInstallationSuccessDate

 

05-10-2013 00-27-33

 

$windowsUpdateObject.Results.LastSearchSuccessDate

 

05-10-2013 00-27-57

You can see the windows automatic basic setting using .Settings property.

$windowsUpdateObject.Settings

05-10-2013 01-03-07

The most cool thing which i have found is ShowSettingsDialog(), method. When you run this method it opens a Windows Update configuration GUI.

$windowsUpdateObject.ShowSettingsDialog()

05-10-2013 01-04-02

I hope you like it.

Thanks for your time and Happy Weekend.

Thanks

Aman Dhally

 

clip_image001 clip_image002 clip_image003 clip_image005  clip_image007

Powershell Techniques : Lock Your Workstation using Powershell.

In my previous post, I created test button on Login Screen changer GUI,  when you clicked on them it lock your workstation.

Capture

I got few queries that which code I used behind the above Test button.  I used the below code to lock the workstation.

 $shell = New-Object -com “Wscript.Shell”

 $shell.Run(“%windir%\System32\rundll32.exe user32.dll,LockWorkStation”)

Just shoot the above code and when you paste the last line, you computer will be locked.

I found it cool! i hope you may also like it.

Aman Dhally


clip_image001 clip_image002 clip_image003 clip_image005clip_image007

PowerShell Techniques : Alias v/s Full Cmdlets usage.

Hi Everyone,

I hope you are enjoying your weekend.

This is my another post on Powershell techniques.

We all love Powershell alias’s they are good and they handy and saved lots of key strokes.  I use Powershell alias a lot. But i have seen few Powershell scripter and they do use Powershell alias in their scripts. 

In Console

1 (fig. 1)

2(fig. 2)

You can see when we use alias in Powershell Console, it use less space and less key strokes,

Good, we know Alias are good so?

In Powershell scripts i don’t prefer to user Powershell alias. Powershell scripts are used by other too , we share the script , so if someone want to change anything or append anything then it is bit difficult  and intercept the code.

So for easy reading, easy interception using full cmdlets are very useful to use in the scripts.

3

Note :- This is my personal opinion and view. 

 

That’s all for now.

Thanks

Aman Dhally

clip_image001 clip_image002 clip_image003 clip_image005clip_image007

PowerShell Techniques : Commenting Scripts, it’s always helpful.

Hi,

I hope you are enjoying my Powershell Techniques series of web post. I always mention in most of my blog posts that, learning or memorizing what cmdlets do is really easy , but it is bit difficult to find techniques.

In my opinion these tips and trick or we can say techniques are universal, these are more then a logic and can apply anywhere and in any scripting language.

When you write Powershell script, it is always handy to add comments on them, as we know, in Powershell we use hash(#) character to add comments.

Why it is important to add comments?

In my point of view it is very important, because:

  1. If you are writing a long script, you will know what you are doing.
  2. If you want to share the script with one else,, they can also understand the script by reading comments.
  3. We can define a separate section for variable,function using comments.
  4. if we are using lots of foreach,if, loops, it is easy to identify ending of each loops.

 

In my scripts I use comments as separator. You can see that, I define a separate space for the Variable, Modules, Functions, So when next time, if i want to add of troubleshoot any functions, at that time i know where to look for them,

12-09-2013 23-09-27

It is always good to add comments above your code, so that you know what that code is doing.,This helps you and helps other to understand your scripts.

12-09-2013 23-16-52

If you are using multiple if/else or other loop and statements, then you can also use #comments in the end of } braces, you that you know which logic is applying on it.

image

Powershell Techniques : Use While loop for rebooting laptop after 30 seconds with a informative message .

Hi,
I hope you all are doing well, I am in Italy for this week and enjoying Italian coffee.
Anyway.
In few of my scripts, before rebooting the laptop, I use Start-Sleep cmdlet to give a pause for 30-60 seconds.
We can do this easily using Start-Sleep 30.But, it is always good to show informative message on the screen so that user can know what is going on, to do that, I use while loop.
See the code below and it is very simple.
#variable
$second = 30
#While Loop
#while our $second variable is greater then 0
While ($second -gt 0 ) {
            # we are minus 1 with every look
            $second
           
            # putting laptop sleeping for one seconds
            sleep 1
           
            # just a message
            Write-Warning “Computer will be restated in  $second”
           
            }

Logically this loop will run 30 times, until $seconds value is not come to zero.

Let’s run the code and the result is. nice informative message about that laptops will be reboot in 30 seconds.


Thanks
I hope you may like this. :)

Thanks
Aman Dhally
clip_image001 clip_image002 clip_image003 clip_image005clip_image007
%d bloggers like this: