Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions src/functions/public/Get-PSModuleTest.ps1

This file was deleted.

67 changes: 67 additions & 0 deletions src/functions/public/Remove-Member.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
function Remove-Member {
<#
.SYNOPSIS
Removes specified members (properties) from an object.

.DESCRIPTION
The Remove-Member function removes specified members (properties) from objects passed through the pipeline.
If the specified properties exist, they are removed; otherwise, a verbose message is displayed.
When the -PassThru switch is used, the modified object is returned.

.EXAMPLE
$obj = [PSCustomObject]@{Name="John"; Age=30; City="New York"}
$obj | Remove-Member -Name "Age" -PassThru

Output:
```powershell
Name City
---- ----
John New York
```

Removes the "Age" property from the PSCustomObject and returns the modified object.

.OUTPUTS
PSCustomObject

.NOTES
The modified object without the specified properties, if -PassThru is used.

.LINK
https://psmodule.io/Object/Functions/Remove-Member
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseShouldProcessForStateChangingFunctions', '',
Justification = 'Sets text in memory'
)]
[CmdletBinding()]
param(
# Accept input from the pipeline.
[Parameter(Mandatory, ValueFromPipeline)]
[System.Object] $InputObject,

# Specify one or more member names to remove.
[Parameter(Mandatory)]
[string[]] $Name,

# When specified, output the modified object.
[Parameter()]
[switch] $PassThru
)

process {
foreach ($obj in $InputObject) {
foreach ($member in $Name) {
if ($obj.PSObject.Properties[$member]) {
$null = $obj.PSObject.Properties.Remove($member)
} else {
Write-Verbose "Property '$member' not found on object of type $($obj.GetType().FullName)."
}
}
if ($PassThru) {
# Only output the object if -PassThru is used.
$obj
}
}
}
}
34 changes: 34 additions & 0 deletions tests/Object.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSReviewUnusedParameter', '',
Justification = 'Required for Pester tests'
)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseDeclaredVarsMoreThanAssignments', '',
Justification = 'Required for Pester tests'
)]
[CmdletBinding()]
param()

Describe 'Object' {
Context 'Function: Remove-Member' {
It 'Removes a member from an object' {
$object = [pscustomobject]@{ Name = 'Test'; Value = 'Value' }
$object | Remove-Member -Name Value
$object.PSObject.Properties | Should -Not -Contain 'Value'
}

It 'Removes multiple members from an object' {
$object = [pscustomobject]@{ Name = 'Test'; Value = 'Value'; Other = 'Other' }
$object | Remove-Member -Name Value, Other
$object.PSObject.Properties | Should -Not -Contain 'Value'
$object.PSObject.Properties | Should -Not -Contain 'Other'
}

It 'Removes properties of classes' {
$object = [System.Net.WebClient]::new()
$object | Add-Member -MemberType NoteProperty -Name Something -Value 'http://example.com'
$object | Remove-Member -Name BaseAddress
$object.PSObject.Properties | Should -Not -Contain 'BaseAddress'
}
}
}
16 changes: 0 additions & 16 deletions tests/PSModuleTest.Tests.ps1

This file was deleted.

Loading