Create a Dictionary in PowerShell

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

12K+ 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

Convert JSON File to CSV File Using PowerShell

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

24K+ 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

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.

Find Computer Product Serial Number Using PowerShell

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

5K+ 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

Find Length of the Longest Path in a DAG Without Repeated Nodes in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:47:33

1K+ Views

Suppose we have one directed acyclic graph represented by the adjacency list. We have to find the longest path in the graph without node repetition.So, if the input is likethen the output will be 4, as the path is 0 -> 1 -> 3 -> 4 -> 2 with length 4.To solve this, we will follow these steps −ans := 0n := node count of graphtable := a list of size n and fill with -1Define a function dfs() . This will take uif table[u] is not -1, thenreturn table[u]p_len := 0for each vectex v in graph[u], dop_len := maximum ... Read More

Length of Longest Increasing Path in a Given Matrix in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:45:43

163 Views

Suppose we have a 2D matrix, we have to find the length of the longest strictly increasing path. To traverse the path we can move up, down, left, or right nor diagonally.So, if the input is like246157339then the output will be 6, as The longest path is [1, 2, 4, 6, 7, 9]To solve this, we will follow these steps −n := row count of matrix , m := column count of matrix moves := a list of pairs to move up, down, left and right [[1, 0], [-1, 0], [0, 1], [0, -1]] Define a function dp() . This ... Read More

Find Length of Longest Palindromic Subsequence in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:45:08

850 Views

Suppose we have a lowercase string s; we have to find the length of the longest palindromic subsequence in s.So, if the input is like s = "aolpeuvekyl", then the output will be 5, as the palindrome is "level".To solve this, we will follow these steps −n := size of sDefine a function dp() . This will take i, jif i is same as j, thenreturn 1otherwise when i > j, thenreturn 0otherwise, if s[i] is same as s[j], thenreturn 2 + dp(i + 1, j - 1)otherwise, return maximum of dp(i + 1, j) and dp(i, j - 1)return ... Read More

Find Length of Longest Path with Even Sum in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:39:35

198 Views

Suppose we have a binary tree. we have to find the length of the longest path whose sum is an even number.So, if the input is like image, then the output will be 5, as the path is like [5, 2, 4, 8, 5], sum = 24(even).To solve this, we will follow these steps −Define a function dfs() . This will take nodeif node is null, thenreturn a pair (0, -inf)(left_0, left_1) := dfs(left of node)(right_0, right_1) := dfs(right of node)if value of node is odd, thenans := maximum of ans, (left_1 + right_0 + 1) and (left_0 + right_1 ... Read More

Find Length of Longest Common Subsequence of Three Strings in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:36:12

685 Views

Suppose we have three strings s1, s2, and s3, we have to find the length of their longest common subsequence.So, if the input is like s1 = "ababchemxde" s2 = "pyakcimde" s3 = "oauctime", then the output will be 4, as the longest common subsequence is "acme".To solve this, we will follow these steps −m := size of s1, n := size of s2, o := size of s3dp := a 3D matrix of size (o + 1) x (n + 1) x (m + 1)for i in range 1 to m, dofor j in range 1 to n, dofor ... Read More

Find Length of Longest Arithmetic Subsequence in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:34:52

366 Views

Suppose we have a list of numbers called nums, we have to find the length of the longest arithmetic subsequence. As we know a sequence S[i] is an arithmetic sequence when S[i+1] - S[i] have the same value for every i in range (0 ≤ i < Size of S - 1).So, if the input is like nums = [1, 4, 7, 10, 13, 20, 16], then the output will be 6, the subsequence [1, 4, 7, 10, 13, 16] is an arithmetic because the difference between each consecutive element is 3.To solve this, we will follow these steps −n := size of arrif n

Advertisements