To set an IP address on a network adapter in Windows, we have the New-NetIPAddress command. This command is part of the NetTcpIp module and is included with PowerShell v5 and later. But, before we change something, we should always check the current configuration. To get the current IP address, we’ll use Get-NetIPAddress.
Sorry , I’m a little confused on bellow two implements as both of them work well.
the first one, $_ it means the current PSobject , and $($) , it means it will calculate the value of current PSobject , or it means it will reference current PSobject ? when we can ignore the first $ in the $($) , and when we can not ?
If you would have a variable Name (for example) and you would put it like “$Name”, then PowerShell will take the value of $Name. But if you would have a hashtable variable with multiple objects in it, PowerShell can’t just translate that into a string. Since PowerShell can’t recognize the dot in a string as a way to mention a property of an object, you need to do this by using the following technique $($HashTable.Property1).
So, $_ and $($_) do the same thing and it’ll both reference the current object in the pipeline. You would need to use the second technique to get a property of an object into a string (double quotes, single is literal).
[quote quote=148902]They both mean the same in this case.
But if you would have a hashtable variable with multiple objects in it, PowerShell can’t just translate that into a string. Since PowerShell can’t recognize the dot in a string as a way to mention a property of an object, you need to do this by using the following technique $($HashTable.Property1).
[/quote]
Sorry , I still not quite understand. Let’s do bellow operation. (BTW , I may understand the difference between double quotes and single quotes in PS )
As bellow , $lsass represent a object , and this object has many properties , how ever the first result of 1…3 return a number which its type is int32 , and it does not have any properties.no need to any evaluate operation when reference to it (no dot to it )
So what I want to say is , we need to use $($lsass.property) to take it into a string , because powershell can not understand the dot in a string in $lsass.property ,right ? the $() will cause $lsass.property evaluate its value , so we need two $ , the first $ and () will force the expression which is $lsass.property to evaluate its value , is it Comme ça?
PS D:\> 1..3 | foreach {"the $_ time of $lsass.ProcessName"}
the 1 time of System.Diagnostics.Process (lsass).ProcessName
the 2 time of System.Diagnostics.Process (lsass).ProcessName
the 3 time of System.Diagnostics.Process (lsass).ProcessName
PS D:\> 1..3 | foreach {"the $_ time of $($lsass.ProcessName)"}
the 1 time of lsass
the 2 time of lsass
the 3 time of lsass
PS D:\> 1..3 | select -First 1 | Get-Member
TypeName: System.Int32
PS D:\> $lsass | Get-Member
TypeName: System.Diagnostics.Process