Author Posts

July 13, 2015 at 9:28 pm

I want to write swapping script which can swap 2 parameters
which cmdlets do I need do hat

July 14, 2015 at 3:35 am

Hi Harshall,
Can you Provide Little More details.
Atleast a sample script.

Regards,
kvprasoon

July 14, 2015 at 6:08 am

Depending on what you are trying to swap, there are a couple of ways to go about it.

(Get-Content $file) | Foreach-Object { $_ -replace $SearchArray[$i], $ReplaceArray[$i] } | Set-Content $file

is one way. If you simple want to swap a word in a string you can use:

$a = $a -replace "CurrentWord", "NewWord"

July 14, 2015 at 9:01 pm

function get-swap{
[CmdletBinding()]
param(
[parameter(mandatory=$true)]$param1,
[parameter(mandatory=$true)]$param2
)

""
"Original Vaues are "+$param1+" & "+$param2
$temp1=$param1
$param1=$param2
$param2=$temp1
""
"swapped Vaues are $param1 & $param2"

}

this I tried and worked
as I am new to this but
my question is that is so simple and this can be done in any programming language with same syntax so how can I reduce the code in power shell Or in other ways how can it get easy than this

July 14, 2015 at 10:02 pm

In PowerShell you can assign a list of values to multiple variables, for example:

$a,$b = 'first','second'

$a will be assigned the first value and $b the second value.
This can be used to swap values on to variables.

Fo your example, try this:

function Get-Swap{
    [CmdletBinding()]
    param(
        [parameter(mandatory=$true)]$param1,
        [parameter(mandatory=$true)]$param2
    )
    "Original Vaues are $param1 & $param2"
    $param1,$param2=$param2,$param1
    "Swapped Vaues are $param1 & $param2" 
}

July 15, 2015 at 12:42 am

@ Simon Wåhlin Thanks Very much that was knowledgeable and exciting 2 🙂

July 15, 2015 at 6:48 am

@simon. That's a new on me as well, thanks for sharing I can check off the "Learned something new" checkbox today. 🙂