Monday, June 16, 2014

#16 : How to play beep and other system sounds?

Below piece of code can be used to play a beep from a Powershell script. I have used in many of scripts where user interaction is required. Presenting a list of different sounds which can be played from a Powershell script.

Beep:
  1. #--Statement to play beep from Powershell Scripts --#    
  2. clear   
  3. [System.Media.SystemSounds]::Beep.Play()   
Hand:
  1. #--Statement to play hand from Powershell Scripts --#    
  2. clear   
  3. [System.Media.SystemSounds]::Hand.Play()   
Asterisk:
  1. #--Statement to play Asterisk from Powershell Scripts --#    
  2. clear   
  3. [System.Media.SystemSounds]::Asterisk.Play()   
    Exclamation:
    1. #--Statement to play Exclamation from Powershell Scripts --#    
    2. clear   
    3. [System.Media.SystemSounds]::Exclamation.Play()   
    Please test and let me know your response. Thanks.

    Monday, June 9, 2014

    #15 : Handling Clipboard with Powershell

    Not sure when you might need to get data from Clipboard. But I thought it might be useful in some cases which I might not think about.
    So, below is the piece of code which can be used.

    Getting Data from Clipboard:

    Below code will look if the Clipboard contains a text data and if this is so, it would display. You may store it into variable and do whatever you like.

    #--Statement to Get Text Data from Clipboard --#   
    if ( [System.Windows.Forms.Clipboard]::GetText() -ne ""  )    
    {    
        [System.Windows.Forms.Clipboard]::GetText()   
       
    else   
    {   
        Write-Output "Clpboard does not contain Text data"    
    }    
    
    

    Setting Data into Clipboard:

    Use below piece of code to set a value to the Clipboard. To confirm if value is set or not, please use above section.

    #--Statement to Set item into Clipboard --#   
    $Data_to_Set="Som DT"    
    if ($Data_to_Set -ne "" )    
    {    
        [System.Windows.Forms.Clipboard]::SetText($Data_to_Set)   
    }   
    
    
    

    Clearing Data from Clipboard:

    Below code can be used to clear data available in clipboard.

    #--Statement to clear the data from clipboard --#    
    [System.Windows.Forms.Clipboard]::Clear()   
    
    

    I hope this will help you. Please comment here if you have any thoughts about it.

    Friday, June 6, 2014

    #14 : Find all the files modified within last 7 days

    Such requirements come to picture many times in different forms. Below is piece of code which can be used for such requirements. I have made it recursive to all subdirectories also.

    1. clear   
    2.   
    3. #--Change the name with directory you want to visit --#    
    4. $dir_to_look="C:\Users\admin\Desktop"    
    5.   
    6. #--You may change the number of days of your choice --#   
    7. $seven_days_backdate=$(Get-Date).AddDays(-7)    
    8.   
    9. #--Find the files which are modified or created within last 7 days --#    
    10. Get-Childitem $dir_to_look -Recurse | `   
    11.         where-object {!($_.psiscontainer)} | `   
    12.         where { $_.LastWriteTime -gt $seven_days_backdate } | `   
    13.         foreach {  Write-Host "$($_.LastWriteTime) :: $($_.Fullname) "  }   

    Wednesday, June 4, 2014

    #12 : How to know Powershell Version?

    People often ask me how to find the Powershell version. The questions might be like -
    How to find the Powershell version?
    What version of Powershell I am using?
    Is there any way to find Powershell version?

    Stop! I am telling you. This is just a small command -
    1. clear   
    2. $Host.Version  
    Output: 
    Major Minor Build Revision
    ----- ----- ----- --------
    2      2    0     1358


    Again, there should no more doubts, Major is the one which you have to look. Ok, let me simplify it.

    1. clear   
    2. $Host.Version | foreach { "Powershell " + $_.Major + ".0" }   
    Output:
    Powershell 2.0 

     
    There should not be any doubts now :).

    Saturday, May 31, 2014

    #13 : Find the path of applications with Powershell

    There is a Windows command "Where" which tells use the location of executable applications. Such as, if you are looking for location of notepad, you can run :

    where Notepad

    Output:
    C:\Windows\System32\notepad.exe
    C:\Windows\notepad.exe

    This helps you much when there are two versions of same executable and you want to know which will get priority. For example, in the above output, there are two entries for notepad.exe, but when you call notepad without providing full path, it will pick the one located in System32.

    The same can be achieved in Powershell with the help of Get-Command cmdlet. This cmdlet will tell you the same output :

    get-command notepad

    CommandType     Name                                                     Definition
    -----------     ----                                                     ----------
    Application     notepad.exe                                              C:\Windows\system32\notepad.exe
    Application     notepad.exe                                              C:\Windows\notepad.exe
    You can adjust the output with format-table cmdlet. Such as, if you want to get output in form of Name and Definition only, you can try below :

    get-command notepad | ft name, definition

    That's all for today. Hope you liked this article. Thanks for reading!

    Thursday, May 22, 2014

    #11 : Creating executable (EXE) from Powershell script using PS2EXE?

    As you all know that Powershell is .NET Framework based scripting language. When a Powershell script runs, scripting is converted ty MSIL (Microsoft Intermediate Langage) and then executed. In short we can say that Powershell script is not interpreted as Batch programming or Unix-based shell scripting. So, internally a Powershell script is converted to machine language then why there is no in-build compiler for Powershell? This is puzzle to me, but this is how it is. Anyways, when we are talking about scripting language, why to think about compiling script to EXE, if you really want to compile, use C# or VB.NET. But you know what, we always think something which is missing. Overall, there is no executable compilation mechanism provided in Powershell (Period!).

    There are some alternatives to compile script to Powershell. One of the tool is PS2EXE. This can be download from CodePlex from below location:

    https://gallery.technet.microsoft.com/PS2EXE-Convert-PowerShell-9e4e07f1

    This is good utility which creates exe from a ps1 file. Before you get so excited to use it, let me tell you some facts about it:
    1. This utility will only encapsulate your script and create exe with that. This is not a compilation of your script. So, you still need to have Powershell installed on the machine where you run.

    2. You code is safe and embedded, so nobody can read what you are doing.

    You can try it if this meets your requirement. Next time, we will talk about another tool similar to this.

    Thank you for reading this article!

    Friday, May 9, 2014

    #10 : Powershell Editor : PowerShell SE

    I found this tool interesting as it give much more with the simple interface. You may download it free from CNET Download as well.

    #112: How to handle xml document in Powershell?

     In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...