How to define a subroutine in PowerShell

There are no subroutines in PowerShell, you need a function:

function RemoveAllFilesByExtenstion    
{
   param(
     [string]$TargetFolderPath,
     [string]$ext
   )  

    ... code... 
}

To invoke it :

RemoveAllFilesByExtenstion -TargetFolderPath C:\Logs -Ext *.log

If you don't the function to return any value make sure you capture any results returned from the commands inside the function.


Pretty simple to convert this to PowerShell:

function RemoveAllFilesByExtenstion([string]$targetFolderPath, [string]$ext)
{
...
}

But the invocation has to use space separated args but doesn't require quotes unless there's a PowerShell special character in the string:

RemoveAllFilesByExtenstion C:\Logs\ .log

OTOH, if the function is indicative of what you want to do, this can be done in PowerShell easily:

Get-ChildItem $targetFolderPath -r -filter $ext | Remove-Item