Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

New object from existing object

$
0
0

Hi,

I'm trying to create a new object from an existing but so far i can only create a pointer to an existing object.

$a=New-Object'object[,]' 5,5
$a[0,0] = 10
$b=$a
$b[0,0] = 9
$a[0,0]
$b[0,0]

This is not working. Both $a and $b are now = 9
The following are not working either

$b=$a.clone()
New-variable b $a
$b=New-object$a

The only thing I've seen working is to create a new object with the same properties as $a then input the data from $a to $b as below

$b=New-Object'object[,]' 5,5
$b[0,0] =$a[0,0]
$b[0,0] = 9

I'm assuming this is due to the fact the object is only stored in the variable, and the new variable becomes a pointer to the same object stored in $a.

This is completly fine when I create objects like this, but the cmdlet import-csv behaves in the exact same way.

What should I do? Is there a easy way to create a object from existing or do i need to manually create a new and systematically populate the data within?

On another completely unrelated topic. I have access denied to post new threads in 'Forums>Using powershell>General'. Can someone fix this please?

Best regards
Freakling 


Viewing all articles
Browse latest Browse all 6937

Trending Articles