Found 463 Articles for PowerShell

How to convert Dictionary to Hashtable in PowerShell?

Chirag Nagrekar
Updated on 15-Dec-2020 07:54:24

869 Views

Like any other data type conversion in PowerShell, we can convert Dictionary to hashtable in a similar way.  We have a below Dictionary for the example called $CityData.Key     Value ---     ----- India      91 Austria    43Its datatype is Dictionary,ExamplePS C:\> $citydata.GetType() | ft -AutoSizeOutputIsPublic IsSerial Name         BaseType -------- -------- ----         -------- True     True     Dictionary`2 System.ObjectTo convert it to the hashtable,$hash = [Hashtable]$citydataOr$hash = [System.Collections.Hashtable]$CityDataDatatype:PS C:\> $hash | ft -AutoSizeOutputName    Value ----    ----- Austria 43 India   91

What is the difference between Dictionary and HashTable in PowerShell?

Chirag Nagrekar
Updated on 15-Dec-2020 07:52:52

974 Views

PowerShell programmers generally prefer the Hashtable over the Dictionary although there are some advantages of using Dictionary. See the difference below.a. Hashtable is easy to declare while the Dictionary is a little complex compared to Hashtable. For example, To create a hashtable, $hash = @{    'Country' = 'India'    'Code' = '91' }To create a Dictionary, $citydata = New-Object System.Collections.Generic.Dictionary"[String, Int]" $citydata.Add('India', 91) b. Hashtable is included in the namespace called Collections while Dictionary is included in the namespace called System.Collections.Generic namespace. Hashtable is non-generic so it can be a collection of different data types and Dictionary belongs to a generic class so it is ... Read More

How to create a Dictionary in PowerShell?

Chirag Nagrekar
Updated on 15-Dec-2020 07:51:06

10K+ Views

To create a dictionary in the PowerShell we need to use the class Dictionary from the .Net namespace System.Collections.Generic. It has TKey and TValue. Its basic syntax isDictionaryTo learn more about this .Net namespace check the link below.https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=net-5.0To create a dictionary we will first create the object for the dictionary object class with the datatypes. In the below example, we need to add the Country name and the country code. So we need String and Int.$countrydata = New-Object System.Collections.Generic.Dictionary"[String, Int]"Once we check the type of the $countrydata variable, it should be the dictionary. For example, ExamplePS C:\> $Countrydata.GetType() | ft -AutoSizeOutputIsPublic ... Read More

How to convert JSON file to CSV file using PowerShell?

Chirag Nagrekar
Updated on 15-Dec-2020 07:48:53

20K+ Views

To convert the JSON file to the CSV file using PowerShell, we need to use the ConvertTo-CSV command as a pipeline.For example, we have a JSON file called PatchingServer.JSON stored at C:\temp and its content is as below.ExamplePS C:\> Get-Content C:\Temp\PatchingServer.json {    "Port": "9000",    "ApplicationName": "TestApp",    "MaintenanceWindow": "Every Saturday",    "BusinessUnit": "IT",    "AppOwner": "Josh",    "AppID": "1jj2221-223443s",    "Location": "EastUS" }We need to convert the above file to the CSV file so we will use the ConvertTo-CSV command but before that, we need the JSON file need to be converted from JSON format to table format using ... Read More

How to find Windows Product Key Using PowerShell?

Chirag Nagrekar
Updated on 15-Dec-2020 07:45:06

4K+ Views

Windows Product key can be retrieved using PowerShell or CMD. To retrieve the product key using PowerShell, we need to query SoftwareLicesingService class and there is a property called OA3xOriginalProductKey which stores the product key.ExampleGet-WmiObject -query `select * from SoftwareLicensingService' | Select OA3xOriginalProductKeyOutputOA3xOriginalProductKey ---------------------- BBBBB-CSDSC-EESSR-KKIDS-AAAAAWe can also query this WMI class using cmd as shown below.wmic path softwarelicensingservice get OA3xOriginalProductKeyCaution: It may or may not work for all the Windows OS. The above is tested in Windows 10.

How to find a Computer product serial number using PowerShell?

Chirag Nagrekar
Updated on 15-Dec-2020 07:43:59

4K+ Views

Generally, Product serial numbers are available at the back of the laptop on the company sticker and we can use the Third-party or manufacturer software to find the Product details. The product serial number can also be found using the BIOS utility or command. We can either use the BIOS command for the cmd or using PowerShell.To get the product serial number using PowerShell, we can use WMI or CIMInstance command. For example, ExampleGet-CimInstance Win32_BIOSWe can also use the WMI command. For example, ExampleGet-WmiObject Win32_BIOSOutputSMBIOSBIOSVersion : F.13 Manufacturer      : AMI Name              : ... Read More

How to set the line breakpoint in the PowerShell?

Chirag Nagrekar
Updated on 20-Nov-2020 08:56:47

486 Views

To set the line breakpoint in the script, we can use the Set-PSBreakpoint command with -Line parameter and need to give the path of the script on which the Line breakpoint needs to set.Consider we have the script below which retrieves the value up to 99, starting from 1, We will set the Line Breakpoint at line number 3 so we will use the below command. Here the script name is WhieScript.ps1 and stored at C:\temp.Set-PSBreakpoint C:\temp\WhileScript.ps1 -Line 3Once you run the above command, you will get the output with the details as shown below.ID Script    Line Command Variable ... Read More

What is the breakpoint in PowerShell?

Chirag Nagrekar
Updated on 20-Nov-2020 08:54:14

214 Views

Breakpoint in the PowerShell is the part of the debugger in the PowerShell commands. We use the breakpoints in PowerShell mainly for troubleshooting and logging purpose.There are three ways to set the breakpoint in PowerShell.Line BreakPoint (Can set the breakpoint for single or multiple lines)Command BreakPoint (Can set the breakpoint for commands or functions)Variable Breakpoint (Can set the breakpoint on a variable or multiple variables).We can’t set the breakpoint for the remote computer. To set the breakpoint for the remote computer, we first need to copy the script to the remote computer and then need to set the breakpoint as ... Read More

How to check if the file is empty using PowerShell?

Chirag Nagrekar
Updated on 20-Nov-2020 08:52:57

6K+ Views

To check if the file is empty using PowerShell, we can use the string method called IsNullorWhiteSpace(). This method provides result true if the file is empty or only contains the white spaces otherwise false.For example, We have a test2.txt text file which has whitespaces.Example[String]::IsNullOrWhiteSpace((Get-content C:\Test2.txt))OutputTrueBut if you have a file like CSV which contains few headers but the data is empty, in that case, Get-Content will show the wrong output because it will consider headers. For example, Example[String]::IsNullOrWhiteSpace((Get-content C:\Temp\NewUsers.csv))OutputFalse Because the file has headers.PS C:\> Get-Content C:\Temp\NewUsers.csv Name, FirstName, Surname, EMPNumber, CountryIn that case, we can use the Import-CSV ... Read More

How to open any file with its default application with PowerShell?

Chirag Nagrekar
Updated on 20-Nov-2020 08:51:40

2K+ Views

To open any file using its default application, we can use the Invoke-Expression command. For example, we want to open a file on the C:\temp and the file name is NewUsers.CSV then you can run the below command.Invoke-Expression C:\Temp\NewUsers.csvThe above command will open the file from that location. If the default application is not set then Windows will ask for the default application to select.If you know any application name and which can be opened with the shortcut then you can directly type the name of the application. For example, Notepad.exe, Calc.exeGenerally, they can be opened directly but this command ... Read More

Advertisements