Posts

Showing posts with the label PowerShell

Error Handling in PowerShell

Image
A good practice while working on any script is to have a mechanism for error handling. Though it may take a little bit of additional time, its rewards are priceless. Before moving to Error handling in PowerShell, first go through what type of errors we may encounter. Error Types There are two types of errors that can be occurred during script execution.  Terminating  Non-terminating  As the name implies, terminating error will stop the program from further execution, whereas a non-terminating error will not stop the execution. An example of terminating error would be a syntax error whereas an example of non-terminating error would be file/path not found. Terminating errors can be caught and handled while Non-terminating errors cannot be caught by try-catch block. Examples:  Terminating error: Script execution halted when error occurred. Non-terminating error:  Write-Host "Still running" executed even after error occurred. Convert Non-terminating errors into Term...

How to generate a GUID in PowerShell

Image
 If you ever need a unique id in your script, you can create GUID (globally unique identifier) In PowerShell 4.0 and earlier you can use [guid] class with its static method. In PowerShell 5.0 and later, you can use New-Guid cmdlet: You can use Get-Member to know its object type. You can also store GUID into a variable like below If you want to get GUID as string, use ToString  method or Guid  property. Both return output of string type, you can verify this using Get-Member .

How to Clean Up PowerShell Script Variables without Restarting PowerShell ISE

In this tutorial, we will see how to clean up variables and modules in PowerShell ISE without restart. PowerShell ISE and Variables values PowerShell ISE does not automatically cleanup it’s variable between manual runs of a script. Due to this, results doesn't came out as we expected. Run below command to clear all variables stored in the session. Remove-Variable * -ErrorAction SilentlyContinue This will take care of clearing all stored variables so we can be sure, script will not inherit any value from a previous run. PowerShell ISE Errors and ModulesPermalink Issue which we face due variable retains value from previous run, can happen with Error buffer also, which can be cleared with the below command. $error .Clear() We can also remove any module loaded by our script with the below command Remove-Module *

How to Import and Export Delimited Files, like CSV, in PowerShell

Image
In this tutorial, we will see how we can use PowerShell to import and export CSV file's data. Managing CSV Files using Import/Export-CSV in PowerShell We have two important cmdlets import-csv/export-csv which are widely used while working with CSV. Before moving to how to manage these type of files, First look at what is CSV file: A Comma Separated Values ( CSV ) file is a plain text file that contains a list of data. These files are often used for exchanging data between different applications. For example, databases and contact managers often support CSV files. A CSV file has a fairly simple structure. It’s a list of data separated by commas as you can see in below screenshot. We have file "email.csv" having email address and name separated by comma. You can use notepad/excel to open thses file. Now lets move back to our main topic, how to manage csv files with PowerShell.  Reading CSV Files with Import-Csv Importing of csv file is pretty straight forward. First move to...

PowerShell Arithmetic Operators

Image
In this tutorial we will see about PowerShell Arithmetic Operators and how PowerShell is dealing with them. Arithmetic operators are used to perform numeric calculations. As well as numeric calculations, the addition operator may also be used with strings, arrays, and hashtables. Multiplication operator may also be used with strings and arrays. Below is the list of PowerShell arithmetic operators. Operator Description Example + Adds numeric values 4 + 6 concatenates a string, arrays, and hash tables “CS” + “Tutorials” – Subtracts numeric values 4 – 2 Makes a number negative -98 * Multiple numeric values 4 * 2 copy string, arrays to the specified number of times “!” * 4 / Divides numeric values 4 / 2 % Gives remainder after division 7 % 3 For live demo, watch below video. I have demonstrated the use of PowerShell arithmetic operators by  creating calculator. Along with operators, Switch statement and While loop also used.  

Copy, Move and Delete files with PowerShell

Image
In this article, let me show you how you can use PowerShell to copy, move and delete files. Before we go through these commands, let me show you how to navigate between different directories/folders. How to change the directory (folder) in Command Prompt (CMD) CD (Change Directory): This command enables you to change the current directory.  When you need to go one folder up, use the "cd.." command. Let’s assume that you want to go back to the Windows folder. Type “cd..” and press Enter on your keyboard. cd takes you to the top of the directory tree when you pass " \ " after it. In this case, to the “C:” drive. If you need to go to a specific folder from this drive run the command "CD Folder". for example, When you need to access the cstutorials folder located in "C:" type "cd cstutorials" as shown below, and then press Enter on your keyboard. CD cmdlet is alias for set-location. you can use set-location also. How to create a new directo...

Controlling Your Code's Flow with PowerShell's Control Statements

Image
PowerShell Control Statements Windows PowerShell provides several ways to control the flow of code. You can use conditional statements to define conditions and the actions to occur when those conditions are met. You can even specify the actions to occur when a condition isn’t met. PowerShell Control statements can be broken down into three sections: Conditional statements Used when you want to execute line of code depending upon some condition. Looping statements Used when you want to perform some task multiple time. Flow control statements Used along with conditional and loop statements to control the flow of execution. Lets go through all, one by one: Conditional statements if statement - execute the script if condition evaluates to true. elseif statement - execute the script if condition evaluates to true and all if, elseif conditions before it false. else statement - execute the scripts that follows it when none of the conditions in if, elseif evaluates to true. Syntax ...