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 :).

    #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...