Posts

Showing posts with the label How to

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