I like to define my variables in a structered fashion. Most MSDN blogs however do not do this.
For example: [object]myObj = ...
Is this the correct default format for all objects in Powershell?
Use PsObject like this:
$o = new-Object PsObject -property @{Name='donald'; Kind='duck' }
You pass a hashtable as argument for -property parameter. Also you can create empty object and add properties later:
$o = New-Object PsObject
$o | Add-Member NoteProperty project myproj.csproj
$o | Add-Member NoteProperty Success $true
You can of course use pipe to Add-Member
$o = New-Object PsObject
# ...
$o |
Add-Member NoteProperty project myproj.csproj -pass |
Add-Member NoteProperty Success $true
If I understand the question, you're asking a couple things:
Powershell will certainly let you specify a type explicitly, but it will also infer types. Note that since all types inherit from System.Object, explicitly specifying [object] in a combined declaration/assignment statement has no value that I can see. The type system will still infer an appropriate child type. For example:
$x = 3
$x.GetType() # Returns 'Int32'
Remove-Variable x
[object] $x = 3
$x.GetType() # Returns 'Int32'
Remove-Variable x
[valuetype] $x = 3
$x.GetType() # Returns 'Int32'
Remove-Variable x
[int] $x = 3
$x.GetType() # Returns 'Int32'
If you split up the declaration and assignment, you can create a variable of type Object:
Remove-Variable x
$x = new-object -TypeName Object
$x.GetType() # Returns 'Object'
...but once you assign a value, the variable gets a new inferred type anyway:
$x = 3
$x.GetType() # Returns 'Int32'
While the type system will happily infer Int32 when you specify Object, explicit types win when the inferred type would be incompatible. For example:
$x = 3 # Gets inferred type 'Int32'
[string] $x = 3 # Gets explicit type 'String'
$x = 'x' # Gets inferred type 'String'
[char] $x = 'x' # Gets explicit type 'Char'
If your question is more geared toward defining and using custom object types, Stej's answer is excellent.
Since Powershell 3 one can also parse HashTables to Custom Objects like:
[PSObject] $Piza = [PSCustomObject] @{
Ingredients = 4
}
Or if you like to define more detailed types in your object you could use the -AsCustomObject Parameter from New-Module
[PSObject] $Piza = New-Module -AsCustomObject -ScriptBlock {
[Guid]$id = [Guid]::NewGuid()
[String] $Name = 'Macaroni'
Function TestFunction() {}
# Dont forget to export your members and functions
# as this is built up as a module and stuffed into
# an object later
Export-ModuleMember -Function * -Variable *
}
As there are no things as classes in posh you can add custom classnames and namespaces to your object that you can query later (pseudo instance ;)
$Piza.PSObject.TypeNames.Insert(0, 'Pizas.Macaroni')
If I understand your question properly, you are asking
In powershell, it is able to create an instance of .Net object by using New-Object. Like this
$ie = New-Object -ComObject InternetExplorer.Application
$ie.Navigate2("http://stackoverflow.com/")
$ie.Visible = $true
The code above create a com object which navigate your ie to stackoverflow.
And, If you want to create your own object, PSObject would be your choice. Like below
$obj = New-Object PSObject -Property @{
Name = "Your Name"
Age = 30
}
When you call the object by using "$obj"
$obj
Age Name
--- ----
30 Your Name
If you are using powershell 3.0, you can get the same result with less typing like below
[PSCustomObject] @{
Name = "Your Name"
Age = 30
}
Creation of the PSObject object is the basis for access to all objects from the scripting language and provides an abstraction for the cmdlet developer and
It differs in different version of powershell..
New-Object PSObject –Property [HashTable]
$Object = New-Object PSObject`
$Object | add-member Noteproperty LineNumber $LineNumber
$Object | add-member Noteproperty Date $TodayDate
You can also create fast objects like that:
$myVar = "" | select column1, column2, column3
this will create object with 3 NoteProperties and you have access to every property on normal basis like
$myVar.column1
P.S. take under consideration that this is Selecte.System.String not System.Management.Automation.PSCustomObject.
Still enough for keeping rows of data, exporting, accessing data cells.
Update: More flexible solution and still easy to implement would be Hashtable created like below.
$myVar2 = @{
column1=value1
column2=value2
column3=value3
}
This will create variable of type System.Collections.Hashtable
with some nice methods
regards hermitagup