PowerShell New-Object Cmdlet
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed Get-Member cmdlet in PowerShell.
https://cloudaffaire.com/powershell-get-member-cmdlet/
In this blog post, we will discuss New-Object cmdlet in PowerShell. The New-Object cmdlet creates an instance of a .NET Framework or COM object. The. NET System Class Type or Progide of a COM object can be defined. You provide the full name of a. NET System class, and the cmdlet returns a reference to that class instance. You can also use the ComObject parameter to construct an instance of a COM object and set a ProgID for the object.
New-Object Cmdlet Syntax:
1 2 3 4 5 |
## New-Object ## [-TypeName] ## [[-ArgumentList] ## [-Property ## [ |
New-Object Cmdlet Argument List:
- –ArgumentList: Specifies an array of arguments to pass to the constructor of the .NET Framework class.
- –ComObject: Specifies the programmatic identifier (ProgID) of the COM object.
- –Property: Sets property values and invokes methods of the new object.
- –Strict: Indicates that the cmdlet generates a non-terminating error when a COM object that you attempt to create uses an interop assembly.
- –TypeName: Specifies the fully qualified name of the .NET Framework class.
PowerShell New-Object Cmdlet:
Create A Dictionary Object In PowerShell:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
###################################### ## PowerShell | Cmdlet | New-Object ## ###################################### ## PowerShell Latest Version (5) ## create a dictionary object $dict = New-Object 'System.Collections.Generic.Dictionary[String,Int]' ## get members of the new dictionary object $dict | Get-Member ## add values to the dictionary $dict.Add("one",1) $dict.Add("two",2) $dict.Add("three",3) $dict |
Create A Custom Object In PowerShell:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
## create a custom object using New-Object $CloudProvider = @{ AWS = 'Amazon Web Services' GCP = 'Google Cloud Platform' Azure = 'Microsoft Azure' } ## create an object using CloudProvider $CloudAffaire = New-Object -TypeName PsObject -Property $CloudProvider $CloudAffaire | Get-Member $CloudAffaire.AWS $CloudAffaire | Add-Member -MemberType NoteProperty -Name Alibaba -value "Alibaba Cloud" $CloudAffaire.Alibaba |
Hope you have enjoyed this article. In the next blog post, we will discuss Select-Object cmdlet in PowerShell.
To get more details on PowerShell, kindly follow below official documentation