< Back
New-ComObject
Post
NAME New-ComObject
SYNOPSIS
Dispose disposable variables, the equivalent of C# using for PowerShell.
SYNTAX
New-ComObject [[-DisposableObject] <__ComObject>] [[-ScriptBlock] <ScriptBlock>] [<CommonParameters>]
DESCRIPTION
Objects which own unmanaged resources (such as network connections and SQL Server database connections) should
have their Dispose method called in order to free up those connections and so the object's memory can later be
released in a timely manner.
New-DisposableObject provides a simple way of managing this using a simple syntax usually consisting of the
instantiation of the object followed by a scriptblock in which it will be used, before being disposed of behind
the scenes.
PARAMETERS
-DisposableObject <__ComObject>
An object which implements System.IDisposable. This is most likely an expression; see the example.
Required? false
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
-ScriptBlock <ScriptBlock>
A script block during which the above object will be used, and after which the object should be disposed of.
Required? false
Position? 2
Default value
Accept pipeline input? false
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
INPUTS
OUTPUTS
NOTES
Care must be taken not to modify variables within the scriptblock which exist in the outer scope. If you must
do so, those variables must use a $script: prefix, otherwise your change may be lost as shown in the second
example.
This function is based largely on work done by Adam Weigert @
http://weblogs.asp.net/adweigert/powers ... -statement
-------------------------- EXAMPLE 1 --------------------------
PS C:\\>New-DisposableObject ($dataSet = New-Object System.Data.DataSet) {
}
Disposes of $dataSet.
-------------------------- EXAMPLE 2 --------------------------
PS C:\\># Try 1
$var = "ABC"
"Before: Var is $var"
New-DisposableObject ($dataSet = New-Object System.Data.DataSet) {
$var = "BCD"
"During: Var is $var"
}
"After: Var is $var"
# Try 2
"Before: Var is $var"
New-DisposableObject ($dataSet = New-Object System.Data.DataSet) {
$script:var = "BCD"
"During: Var is $var"
}
"After: Var is $var"
Showing the effects of variables within the scriptblock.
RELATED LINKS
SYNOPSIS
Dispose disposable variables, the equivalent of C# using for PowerShell.
SYNTAX
New-ComObject [[-DisposableObject] <__ComObject>] [[-ScriptBlock] <ScriptBlock>] [<CommonParameters>]
DESCRIPTION
Objects which own unmanaged resources (such as network connections and SQL Server database connections) should
have their Dispose method called in order to free up those connections and so the object's memory can later be
released in a timely manner.
New-DisposableObject provides a simple way of managing this using a simple syntax usually consisting of the
instantiation of the object followed by a scriptblock in which it will be used, before being disposed of behind
the scenes.
PARAMETERS
-DisposableObject <__ComObject>
An object which implements System.IDisposable. This is most likely an expression; see the example.
Required? false
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
-ScriptBlock <ScriptBlock>
A script block during which the above object will be used, and after which the object should be disposed of.
Required? false
Position? 2
Default value
Accept pipeline input? false
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
INPUTS
OUTPUTS
NOTES
Care must be taken not to modify variables within the scriptblock which exist in the outer scope. If you must
do so, those variables must use a $script: prefix, otherwise your change may be lost as shown in the second
example.
This function is based largely on work done by Adam Weigert @
http://weblogs.asp.net/adweigert/powers ... -statement
-------------------------- EXAMPLE 1 --------------------------
PS C:\\>New-DisposableObject ($dataSet = New-Object System.Data.DataSet) {
}
Disposes of $dataSet.
-------------------------- EXAMPLE 2 --------------------------
PS C:\\># Try 1
$var = "ABC"
"Before: Var is $var"
New-DisposableObject ($dataSet = New-Object System.Data.DataSet) {
$var = "BCD"
"During: Var is $var"
}
"After: Var is $var"
# Try 2
"Before: Var is $var"
New-DisposableObject ($dataSet = New-Object System.Data.DataSet) {
$script:var = "BCD"
"During: Var is $var"
}
"After: Var is $var"
Showing the effects of variables within the scriptblock.
RELATED LINKS