Need help

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

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

Regards,
kvprasoon

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”

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

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" 
}

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

@Simon. That’s a new on me as well, thanks for sharing I can check off the “Learned something new” checkbox today. :slight_smile: