Change or rename keys

I know the script below is incomplete I just cant find what I need to do to complete or this script might be wrong also. The objective was to replace “/users/” with “/userstest/” but the “/Home/users/” gets changed also into “/Home/userstest/”. I have made few but all wrong and couldn’t get the right script. I hope somebody can help.

$targetfolder = foreach ($data in $filterdata) {

 $replacefolder = @{
    "/users/" = "/userstest/"
    "/Home/users/" = "/home_user_test/"
    "/Home/" = "/hometest/"

}

    [PSCustomObject]@{
        FOLDER = $replacefolder
        DAY = $data.DAY
    }

}
$targetfolder

You need to tell us what you’re trying to do. The title of your post and your code make no sense.

Apologies, here’s the example that created but not getting the result.
Just want to replace some values in the table. but words has same words in other values.

filter Server and the target folder only

if ($env:COMPUTERNAME -eq “Server2”) {$ftpname = “eftp.kamotegroup.com”}
$importdata = Import-Excel “D:\Users\users\SFTP_Folders.xlsx”
$filterdata = $importdata | Where-Object {$.server -eq “$ftpname” -and $.action -eq “Activate”}

$targetfolder = @()
foreach ($data in $filterdata) {

$replacefolder = if ($data.FOLDER -like "/Home/users/*")
        {
        $_ -replace "/Home/users/", "/home_user_test/"}
            Else
            {if ($data.FOLDER -like "/Home/*"){
                $_ -replace "/Home/", "/hometest/"}
                    Else
                    {if ($data.FOLDER -like "/Users/*"){
                        $_ -replace "/Users/", "/userstest/"}}}
   
       $targetfolder += [PSCustomObject]@{
        FOLDER = $replacefolder
        DAY = $data.DAY
        }

}
$targetfolder

Note: to properly format code, please use the </> button rather than the quote button.

This should work:

# Sample data for testing.
$filterData = @(
    @{
        FOLDER = '/Home/Users/Foo'
        Day    = 'yesterday'
    }
    @{
        FOLDER = '/Home/Bar'
        Day    = 'today'
    }
    @{
        FOLDER = '/Users/Baz'
        Day    = 'tomorrow'
    }
)

$targetFolder = foreach ($data in $filterData) {

    if ($data.FOLDER -like '/Home/users/*') {
        $data.FOLDER = $data.FOLDER -replace '/Home/users/', '/home_user_test/'
    }
    elseif ($data.FOLDER -like '/Home/*') {
        $data.FOLDER = $data.FOLDER -replace '/Home/', '/hometest/'
    }
    elseif ('/Users/*') {
        $data.FOLDER = $data.FOLDER -replace '/Users/', '/userstest/'
    }
       
    [PSCustomObject]@{
        FOLDER = $data.FOLDER
        DAY    = $data.DAY
    }
    
}
1 Like

I actually found the solution after so many hours. But I found yours simple and easy so Ill be using yours. Thank you

I’ll take note also about the format

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.