PowerShell Tips and Tricks

Few things I like about PowerShell.Note for readers.PS means PowerShell and I am using PowerShell v3.0 so it should work on version v3.0 and above

  1. Parameters can be passed at run time

  2. This means reproducibility. It is similar to the concept of a global variable.These parameters are declared at the top of the script. Below is an example. In the example we have parameters like environment,server,no of files to process,extension of the files to process. Environment can be development, testing or production or any other depending what terminology is used in your organization. If environment is dev somewhere in the code you can have a if-else logic where if it is production it is directly deployed on the server.

    param(
    [string]$env="dev"

    [int]$server="10.0.0.1"

    [int]$files="100"

    [String]$extension=".pdf"

    }
    processFiles(argument1,argument2,...argument n)
    {
    Rest of the code here
    }
  3. PS Modules
  4. PowerShell is a great improvement over Command Line Interface. Some of the modules that are worth mentioning including their
    details.
    1. Get-ChildItem
    2. This has several switches like the recurse, include, filter, exclude argument. For example if you want to get the contents of a
      folder but you only want csv files.
      Get-ChildItem -Path "path to your folder" -Recurse -Filter ".csv"
    3. Sort-Object
    4. Export-CSV
    5. Export-CSV is a very helpful cmdlet (yes these modules are called cmdlet in powershell). You can export objects too ! I will explain
      .Net Objects in a while. Another thing that I like with this cmdlet is appending to an existing document.
      Export-CSV -Path "your path"
  5. Piping and other associated keywords in PowerShell
  6. .NET framework