2

I was designing my own function which renames the .ps1 file to .psm1 with the same name of .ps1 file.

Example: Add-UdfUser.ps1 will become Add-UdfUser.psm1.

It works completely fine. It changes the name with same name of .ps1 file.

Only problem is that when you echo $PSFileName renamed to $NewName or echo the file $PSFileName does not exist it doesn't gets printed on the screen unless you use break statement.

Can somebody tell me why is this happening and any alternative method to fix this?

Here is the code for the reference -

Function Rename-UdfFunction{
[cmdletbinding()]
Param(
[Parameter(Mandatory)]
[String]$PSFileName,
[Parameter(Mandatory)]
[String]$UdfModulePath
)
[String]$SplitName = $PSFileName -split '.ps1' | ForEach-Object {
if ((get-childitem -name -path $UdfModulePath) -match $PSFileName ){
[String]$NewName = $_.TrimEnd()+'.psm1'
Rename-Item -Path $UdfModulePath\$PSFileName -NewName $NewName
echo "$PSFileName renamed to $NewName"
#break
}else{
echo "The file $PSFileName doesnot exist"
#break
}


}


}
3
  • My really sorry. I noticed the $UdfModulePath typo when I was posting this question so I corrected it from $UdfMudulePath to $UdfModulePath one by one on my phone and i forgot to change one. I corrected it now. I request you to check it again.
    – user60845
    Mar 24, 2021 at 7:41
  • It's because what you are doing is syntactically wrong. Variable names don't match, expected values to be passed are not valid, you are only asking for one file name, but then looping, why? Etc...
    – postanote
    Mar 24, 2021 at 7:43
  • @postanote got it. Thankyou so much sir for replying.
    – user60845
    Mar 24, 2021 at 7:45

1 Answer 1

2

Here is a refactor to deal with your use case. with validated results per phase.

Function Rename-UdfFunction
{
    [cmdletbinding(SupportsShouldProcess)]
    Param
    (
        [Parameter(Mandatory)][String]$PSFileName,
        [Parameter(Mandatory)][String]$UdfModulePath
    )


    If (Test-Path -path "$UdfModulePath\$PSFileName")
    {
        "$PSFileName already exists.  Renaming file."
        $NewName = "$($PSFileName.Split('.')[0]).psm1"
        Rename-Item -Path "$UdfModulePath\$PSFileName" -NewName $NewName 
        "Rename of $PSFileName to $NewName completed."
    }
    Else
    {Write-Warning -Message "The file $PSFileName does not exist in the target $UdfModulePath." }
}

Clear-Host
Rename-UdfFunction -PSFileName 'TestFile.ps1' -UdfModulePath 'C:\Temp' -WhatIf

# Results
<#
WARNING: The file TestFile.ps1 does not exist in the target C:\Temp.
#>

# after copying the file to that location.
<#
TestFile.ps1 already exists.  Renaming file.
What if: Performing the operation "Rename File" on target "Item: C:\Temp\TestFile.ps1 Destination: C:\Temp\TestFile.psm1".
#>

# Results
<#
TestFile.ps1 already exists.  Renaming file.
Rename of TestFile.ps1 to TestFile.psm1 completed.
#>
3
  • Sir you are so good at this. My mind just blown up with your answer.
    – user60845
    Mar 24, 2021 at 8:14
  • No worries mate, we've all been there at one point or the other, in our past, present, and future. It's all about discovery. ABL (always be learning)
    – postanote
    Mar 24, 2021 at 8:29
  • true sir you are right sir.
    – user60845
    Mar 24, 2021 at 11:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .