Found 2043 Articles for Microsoft Technologies

How to check if PSCustomObject is empty in PowerShell?

Chirag Nagrekar
Updated on 08-Feb-2021 07:41:09

3K+ Views

To check if the PSCustomObject is empty or not in PowerShell, we need to check the fields of the PSCustomObject. Consider the below example, Example$output = [PSCustomObject]@{    Name = 'John'    City = 'New York'    Country = 'US'    Company = 'Alpha'   } $output1 = [PSCustomObject]@{    Name = ''    City = ''    Country = ''    Company = '' }OutputPS C:\WINDOWS\system32> $output Name    City     Country    Company ----    ----     -------    ------- John    New York  US        Alpha PS C:\WINDOWS\system32> $output1 Name    City     Country    Company ... Read More

How to delete all the file contents using PowerShell?

Chirag Nagrekar
Updated on 08-Feb-2021 07:38:06

3K+ Views

If you want to delete the entire text file content using PowerShell, then we can use the Clear-Content command. For example, we have the below text file called Locations.txt on the C:\Temp path. You can check the content using the below file.Get-Content C:\temp\locations.txtTo clear the file content, we can use the below command.Clear-Content C:\Temp\locations.txt -Force-Force switch is used to clear the contents without user confirmation.When you use this command with the pipeline in the Get-Content command, it will generate an IO Exception error that the file is in use because we are already retrieving the contents using Get-Content and then ... Read More

How to get installed windows update using PowerShell?

Chirag Nagrekar
Updated on 08-Feb-2021 07:35:21

12K+ Views

To get the installed windows updates using PowerShell, we can use the Get-Hotfix command. This command gets the hotfixes and updates that are installed on the local and the remote computer.This command is the part of Microsoft.Management.PowerShell utility.ExampleGet-HotFixOutputPS C:\> Get-HotFix Source Description HotFixID InstalledBy InstalledOn ------ ----------- -------- ----------- ----------- LABMACHINE... Update KB3191565 LABMACHINE2K12\Ad... 1/15/2021 12:00:00 AM LABMACHINE... Update KB2999226 LABMACHINE2K12\Ad... 1/13/2021 12:00:00 AMIn the above output, you can see the SourceMachine Name, HotfixID, InstalledBy, and the Installed Date.You can also sort it by the InstalledOn parameter. For example, ExampleGet-HotFix | Sort-Object InstalledOn -DescendingThis command supports the ComputerName parameter which ... Read More

Difference between Test-Path and Resolve-Path in PowerShell?

Chirag Nagrekar
Updated on 08-Feb-2021 07:33:02

558 Views

Test-Path command checks if the particular path exists or not and returns the Boolean output (True or False) while Resolve-Path command displays the particular directory if exists otherwise throws an exception. For example, For the path to exist, ExamplePS C:\> Test-Path C:\Temp\ True PS C:\> Resolve-Path C:\Temp\ Path ---- C:\Temp\For the path doesn’t exist, PS C:\> Test-Path C:\Temp11\ False PS C:\> Resolve-Path C:\Temp11\ Resolve-Path : Cannot find path 'C:\Temp11\' because it does not exist. At line:1 char:1 + Resolve-Path C:\Temp11\ + ~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\Temp11\:String) [Resolve-Path], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound, Microsoft.PowerShell.Commands.ResolvePathCommandResolve-Path is also used to get the ... Read More

How to retrieve windows registry keys and values using PowerShell?

Chirag Nagrekar
Updated on 08-Feb-2021 07:31:24

5K+ Views

To browse through the registry in PowerShell, we can use the Get-ChildItem command. For example to get all keys from the path HKLM:\Hardware we can use the below command.Get-ChildItem HKLM:\HARDWAREOr you can set the location and use the dir (get-ChildItem or ls) command to browse the path.ExamplePS C:\> Set-Location HKLM:\HARDWARE PS HKLM:\HARDWARE> dirOutputHive: HKEY_LOCAL_MACHINE\HARDWARE Name    Property ----    -------- ACPI DESCRIPTION DEVICEMAP RESOURCEMAPTo get the properties of the key, use the Get-ItemProperty command.ExampleSet-Location 'HKLM:\SOFTWARE\VMware,  Inc.' Get-ItemProperty '.\VMware Drivers'Outputefifw.status   : 1|1.1.0.0.0.1|oem2.inf vmxnet3.status : 1|1.1.8.16.0.1|oem3.inf pvscsi.status   : 1|1.1.3.15.0.1|oem4.inf vmusbmouse.status : 1|1.12.5.10.0.1|oem5.inf vmmouse.status : 1|1.12.5.10.0.1|oem6.infRead More

How to delete registry Key using PowerShell?

Chirag Nagrekar
Updated on 31-Oct-2023 22:00:48

51K+ Views

To delete the registry key using PowerShell, we can use the Remove-Item command. Remove-Item command removes the registry key from the path specified. For example, we have the registry key name NodeSoftware stored at the path HKLM, under the Software key.To delete the key we will use the below command.Remove-Item -Path HKLM:\SOFTWARE\NodeSoftware -Force -VerboseWe can also use the Get-Item command to retrieve the Key name and then use the Remove-Item after pipeline.Get-Item HKLM:\SOFTWARE\NodeSoftware | Remove-Item -Force -Verbose

How to delete registry key value (property) using PowerShell?

Chirag Nagrekar
Updated on 08-Feb-2021 07:28:37

10K+ Views

To delete the registry key value using PowerShell, we can use the Remove-ItemProperty command. Suppose we have the registry NodeSoftware and its Property is AppSecurity. We need to delete its key using the Remove-ItemProperty command.PS C:\> Get-Item HKLM:\SOFTWARE\NodeSoftware Hive: HKEY_LOCAL_MACHINE\SOFTWARE Name    Property ----    -------- NodeSoftware    AppSecurity : 1To delete the registry key, PS C:\>Remove-ItemProperty HKLM:\SOFTWARE\NodeSoftware\ -Name AppSecurity -Force -Verbose VERBOSE: Performing the operation "Remove Property" on target "Item: HKEY_LOCAL_MACHINE\SOFTWARE\NodeSoftware\ Property: AppSecurity".You can also delete property by setting the location. For example, ExamplePS C:\> Set-Location HKLM:\SOFTWARE\NodeSoftware PS HKLM:\SOFTWARE\NodeSoftware> Remove-ItemProperty -Path . -Name AppSecurity -Force -VerboseTo remove Item property using Pipeline, Get-Item HKLM:\SOFTWARE\NodeSoftware | Remove-ItemProperty -Name AppSecurity -Force -VerboseRead More

How to dismount the ISO file using PowerShell?

Chirag Nagrekar
Updated on 25-Jan-2021 07:25:55

3K+ Views

To dismount the ISO file on the Windows Server using PowerShell, we need to use the Dismount−DiskImage command. When you use this command, you need to use the same path used for mounting the disk image.In this example, we have a disk mounted on E: on the local server, that we can check using the windows explorer or cmdlets.We have the source ISO image stored at F: drive, so we can dismount the image using the below command.ISO Disk will be mounted from the E:To dismount the disk on the remote server, we can use the CIMSession command.$sess = New−CimSession ... Read More

How to mount the ISO file using PowerShell?

Chirag Nagrekar
Updated on 25-Jan-2021 07:26:11

3K+ Views

To mount the ISO file in Windows using PowerShell, we can use the Mount−DiskImage command. The below command will mount the image file stored at the C:\ISO location.Mount−DiskImage −ImagePath "C:\ISOs\Windows_Server_2016_Datacenter_EVAL_en−us_14393_refresh (1).ISO"OutputAttached : True BlockSize : 0 DevicePath : \.\CDROM0 FileSize : 6972221440 ImagePath : C:\ISOs\Windows_Server_2016_Datacenter_EVAL_en-us_14393_refresh (1).ISO LogicalSectorSize : 2048 Number : 0 Size : 6972221440 StorageType : 1 PSComputerName :You can also check from Windows Explorer that the disk is mounted.To mount on the remote computer, we can use CIMSession as a remote session and mount the disk.$sess = New−CimSession −ComputerName Labmachine2k12 Mount−DiskImage −CimSession $sess −ImagePath F:\Windows_Server_2016_Datacenter.isoIn the above example, ... Read More

How to convert JSON object to Hashtable format using PowerShell?

Chirag Nagrekar
Updated on 25-Jan-2021 07:26:25

5K+ Views

PowerShell 7 supports the -AsHashtable parameter in the ConvertFrom−JSON command to convert the JSON to hashtable directly and that is a great feature. Consider we have the below JSON file, We can use the pipeline command ConvertFrom−JSON to convert the JSON file to the custom table format and with the −AsHashtable parameter to convert the custom object to the hashtable.PS C:\Temp> Get-Content .\testsevent.json | ConvertFrom-Json -AsHashtable Name Value ---- ----- Events {602d9444−d2cd−49c7−8624−8643e7171297} DocumentIncarnation 0To retrieve the data, PS C:\Temp> $out = Get−Content .\testsevent.json | ConvertFrom−Json −AsHashtable PS C:\Temp> $out.EventsOutputPS C:\Temp> $out.Events Name Value ---- ----- Description Host server is undergoing ... Read More

Advertisements