SOLVED

Return command is not coming out of function and not returning value

%3CLINGO-SUB%20id%3D%22lingo-sub-1052126%22%20slang%3D%22en-US%22%3EReturn%20command%20is%20not%20coming%20out%20of%20function%20and%20not%20returning%20value%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1052126%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20using%20below%20code%3C%2FP%3E%3CP%3E1)%20To%20verify%20whether%20given%20input%20text%20is%20in%20JSON%20format%3C%2FP%3E%3CP%3Ethen%3C%2FP%3E%3CP%3E2)%20User%20acceptance%20on%20the%20file%20selected%20as%20per%20its%20version.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMeans%2C%20if%20i%20give%20wrong%20JSON%20format%20in%20the%20input%20file%20name%2C%20it%20will%20ask%20for%20a%20correct%20formatted%20file%20so%20that%20I%20will%20verify%20the%20JSON%20code%20in%20file%2C%20correct%20it%20and%20then%20input%20again.%20Then%20it%20will%20verify%20the%20version%20of%20the%20JSON%20file%20and%20asks%20for%20user%20acceptance.%20Post%20user%20acceptance%2C%20return%20value%20otherwise%20ask%20for%20alternate%20file%20with%20required%20version.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%2C%20lets%20say%20if%20i%20took%20'n'%20iterations%20to%20get%20correct%20JSON%20format%2C%20post%20giving%20correct%20formatted%20JSON%20as%20input%2C%20%3CFONT%20color%3D%22%23FF0000%22%3Euser%20acceptance%20is%20also%20iterating%20'n'%20times%20even%20though%20it%20is%20accepted%20initial%20time%20itself.%3C%2FFONT%3E%20Means%2C%20'return'%20is%20not%20coming%20out%20of%20the%20function%20(which%20should%20actually%20come%20out%20of%20the%20function%20as%20per%20my%20knowledge).%20Please%20correct%20me%20if%20my%20assumption%20about%20'return'%20is%20wrong%20or%20correct%20the%20code.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENote%3A%20By%20using%20another%20function%20for%20JSON%20validation%20I%20got%20the%20issue%20fixed%20but%20I%20don't%20want%20to%20follow%20that%20process.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E%3CU%3E%3CSTRONG%3ECode%3C%2FSTRONG%3E%3C%2FU%3E%3A%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3Efunction%20Cfile%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3E%7B%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3Eparam(%5BString%5D%24title)%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3Etry%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3E%7B%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3E%24filepath%20%3D%20Read-host%20%22Enter%20the%20path%20of%20the%20file%22%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3E%24JSONformat%20%3D%20gc%20%24filepath%20%7C%20ConvertFrom-Json%20-ErrorAction%20Stop%3B%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3EWrite-Host%20%22Valid%20JSON%20format%22%20-ForegroundColor%20Green%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3E%7D%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3Ecatch%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3E%7B%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3EWrite-Host%20%22Provided%20text%20is%20not%20a%20valid%20JSON%20string%22%20-ForegroundColor%20Red%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3ECfile%20-title%20%22Select%20Correct%20Config%20file%22%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3E%7D%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3EWrite-Host%20%22Entered%20baseline%20version%20is%22%20%24JSONformat.version%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3E%24confirm%20%3D%20Read-Host%20%22Is%20that%20correct%3F%3F%20(Y%2FN)%22%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3ESwitch(%24confirm)%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3E%7B%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3EY%7B%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3E%24done%20%3D%20%22Good%20job%22%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3EBreak%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3E%7D%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3EDefault%7B%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3EWrite-Host%20%22Select%20Correct%20Config%20file%22%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3Ecfile%20-title%20%22Select%20Correct%20Config%20file%22%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3E%7D%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3E%7D%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3EReturn(%24Done)%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%230000FF%22%3E%7D%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E%24Required%20%3D%20Cfile%20%22Select%20required%20JSON%22%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CU%3E%3CSTRONG%3EInput%20JSON%20file%20(correct%20format)%3C%2FSTRONG%3E%3C%2FU%3E%3A%3C%2FP%3E%3CP%3E%7B%3CBR%20%2F%3E%22version%22%3A%2087%2C%3CBR%20%2F%3E%22TestingJSON%22%3A%20%5B%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%22type%22%3A%20%22string%22%2C%3CBR%20%2F%3E%22category%22%3A%20%22crytical%22%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%5D%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CU%3E%3CSTRONG%3ENote%3C%2FSTRONG%3E%3C%2FU%3E%3A%20To%20give%20wrong%20JSON%20format%20as%20input%2C%20may%20be%20you%20can%20remove%20any%20Comma%2C%20brace%2C%20quotation%20..%20etc%20from%20above%20file%20(if%20you%20are%20not%20much%20familiar%20about%20JSON.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1052126%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1057658%22%20slang%3D%22en-US%22%3ERe%3A%20Return%20command%20is%20not%20coming%20out%20of%20function%20and%20not%20returning%20value%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1057658%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F412611%22%20target%3D%22_blank%22%3E%40SBVRaja%3C%2FA%3E%26nbsp%3BI%20several%20gotchas%20which%20may%20explain%20the%20behavior%20you're%20seeing...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CUL%3E%3CLI%3EUsing%20recursive%20calls%20to%20CFile%20is%20probably%20not%20the%20best%20approach%2C%20especially%20from%20inside%20the%20catch%20%7B%7D%20block.%3C%2FLI%3E%3CLI%3EThe%20%24Done%20variable%20is%20never%20properly%20initialized%2C%20and%20the%20only%20possible%20return%20value%20is%20%22Good%20job%22%20for%20%24Required.%3C%2FLI%3E%3CLI%3EI%20might%20recommend%20moving%20any%20statements%20referencing%20%24JSONformat%20inside%20your%20try%20%7B%7D%20block.%3C%2FLI%3E%3CLI%3EYour%20%24title%20argument%20to%20the%20CFile%20function%20is%20never%20used.%3C%2FLI%3E%3CLI%3EIf%20possible%2C%20avoid%20using%20Write-Host%3B%20and%20instead%20try%20using%20either%20Write-Verbose%2C%20Write-Output%2C%20Write-Information%2C%20Write-Warning%2C%20etc.%3C%2FLI%3E%3CLI%3ESince%20your%20Switch()%20statement%20only%20handles%202%20conditions%2C%20an%20if()%20%7B%7D%20else%20%7B%7D%20block%20might%20be%20cleaner.%3C%2FLI%3E%3CLI%3EReturn(%24Done)%20is%20mostly%20equivalent%20to%3A%20%24Done%3B%20Return%3B%20Since%20this%20is%20the%20last%20statement%20in%20your%20function%2C%20Return%20is%20really%20unnecessary.%3C%2FLI%3E%3C%2FUL%3E%3CP%3EThat%20said%2C%20here's%20one%20possible%20re-write%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Efunction%20Cfile%0A%7B%0A%20%5Bcmdletbinding()%5D%0A%20param(%5Bstring%5D%24title%20%3D%20'Enter%20the%20path%20of%20the%20file')%0A%20%0A%20begin%20%7B%0A%20%20%24done%20%3D%20'Bad%20job'%0A%20%7D%0A%20%0A%20process%20%7B%0A%20%20Do%20%7B%0A%20%20%20try%20%7B%0A%20%20%20%20%24filepath%20%3D%20Read-host%20%24title%0A%20%20%20%20%24JSONformat%20%3D%20(get-content%20-Path%20%24filepath%20-ErrorAction%20Stop%20%7C%20ConvertFrom-Json%20-ErrorAction%20Stop)%0A%20%20%20%20Write-Verbose%20%22Valid%20JSON%20format%60nEntered%20baseline%20version%20is%3A%20%24(%24JSONformat.version)%22%0A%20%20%20%7D%20catch%20%7B%0A%20%20%20%20Write-Warning%20'Missing%20file%2C%20or%20provided%20text%20is%20not%20a%20valid%20JSON%20string'%0A%20%20%20%7D%0A%0A%20%20%20%24confirm%20%3D%20Read-Host%20'Is%20that%20correct%3F%3F%20(Y%2FN)'%0A%20%20%20if%20('Y'%20-eq%20%24confirm)%20%7B%0A%20%20%20%20%24done%20%3D%20'Good%20job'%0A%20%20%20%7D%20else%20%7B%0A%20%20%20%20%24title%20%3D%20'Select%20Correct%20Config%20file'%0A%20%20%20%7D%0A%20%20%7D%20Until%20('Bad%20job'%20-ne%20%24done)%0A%20%0A%20%20%24done%0A%20%7D%0A%20%0A%20end%20%7B%7D%0A%7D%0A%0A%24Required%20%3D%20Cfile%20-Verbose%0AWrite-Output%20%24Required%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20this%20helps.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1061365%22%20slang%3D%22en-US%22%3ERe%3A%20Return%20command%20is%20not%20coming%20out%20of%20function%20and%20not%20returning%20value%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1061365%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F413959%22%20target%3D%22_blank%22%3E%40JRCigars%3C%2FA%3E%26nbsp%3BThanks%20for%20looking%20into%20this%20and%20your%20time%20JRCigars.%20I%20really%20appreciate%20that.%20My%20actual%20problem%20here%20is%20not%20to%20find%20alternate%20solutions%20(As%20mentioned%2C%20I%20already%20had%20an%20alternate%20solution%20and%20I%20also%20believe%20there%20are%20many%20other%20solutions%20too%20just%20like%20Do-While%20you%20used%20which%20is%20also%20a%20great%20approach%20which%20I%20didn't%20think).%20But%20my%20actual%20cause%20here%20is%20to%20understand%20the%20behavior%20of%20'Return'%20and%20put%20a%20'%3F'%20at%20where%20ever%20I%20don't%20have%20a%20proper%20answer%3CBR%20%2F%3EConsidering%20this%2C%20here%20are%20my%20responses%20to%20your%207%20points%20with%20'%3F'%20..%20%3A)%3C%2Fimg%3E%3CBR%20%2F%3E1)%20Using%20recursive%20calls%20to%20CFile%20is%20probably%20not%20the%20best%20approach%2C%20especially%20from%20inside%20the%20catch%20%7B%7D%20block.%3CBR%20%2F%3ERaja%3A%20Understood.%20I%20may%20agree%20with%20you%20on%20this.%20But%20WHY%3F%3CBR%20%2F%3E2)%20The%20%24Done%20variable%20is%20never%20properly%20initialized%2C%20and%20the%20only%20possible%20return%20value%20is%20%22Good%20job%22%20for%20%24Required.%3CBR%20%2F%3ERaja%3A%20I%20don't%20think%20it%20is%20a%20big%20issue%20(in%20fact%20not%20a%20issue%20at%20all).%20In%20fact%2C%20the%20code%20that%20i%20gave%20is%20very%20simplified%20version%20of%20actual%20bulk%20code.%20These%20%24Done%2C%20%24title%2C%20the%20function%20CFile%20itself%20are%20all%20its%20simplified%20versions.%3CBR%20%2F%3E3)%20I%20might%20recommend%20moving%20any%20statements%20referencing%20%24JSONformat%20inside%20your%20try%20%7B%7D%20block%3CBR%20%2F%3ERaja%3A%20I%20completely%20agree.%20This%20is%20actually%20simplifying%20the%20issue%20completely.%3CBR%20%2F%3E4)%20Your%20%24title%20argument%20to%20the%20CFile%20function%20is%20never%20used.%3CBR%20%2F%3ERaja%3A%20I%20don't%20think%20its%20of%20big%20issue%20(in%20fact%20not%20a%20issue%20at%20all).%20In%20actual%20code%2C%20I%20generally%20use%20this%20variable%20to%20display%20while%20reading%20%24filepath%20in%20try%7B%7D%20block%20as%20this%20function%20'Cfile'%20i%20use%20for%20multi%20purpose.%20In%20fact%2C%20the%20code%20that%20i%20gave%20is%20very%20simplified%20version%20of%20actual%20bulk%20code.%20These%20%24Done%2C%20%24title%2C%20the%20function%20CFile%20itself%20are%20all%20its%20simplified%20versions.%3CBR%20%2F%3E5)%20If%20possible%2C%20avoid%20using%20Write-Host%3B%20and%20instead%20try%20using%20either%20Write-Verbose%2C%20Write-Output%2C%20Write-Information%2C%20Write-Warning%2C%20etc.%3CBR%20%2F%3ERaja%3A%20I%20would%20consider%20it.%20I%20will%20study%20this%20alternate%20approach%3CBR%20%2F%3E6)%20Since%20your%20Switch()%20statement%20only%20handles%202%20conditions%2C%20an%20if()%20%7B%7D%20else%20%7B%7D%20block%20might%20be%20cleaner%3CBR%20%2F%3ERaja%3A%20I%20agree.%20But%20in%20actual%20code%2C%20there%20are%203%20more%20conditions%20that%20it%20actually%20verifies.%3CBR%20%2F%3E7)%20Return(%24Done)%20is%20mostly%20equivalent%20to%3A%20%24Done%3B%20Return%3B%3CBR%20%2F%3ERaja%3A%20I%20agree.%20But%20with%20the%20use%20of%20Return%20why%20the%20engine%20is%20not%20moving%20out%20of%20function%3F%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1062642%22%20slang%3D%22en-US%22%3ERe%3A%20Return%20command%20is%20not%20coming%20out%20of%20function%20and%20not%20returning%20value%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1062642%22%20slang%3D%22en-US%22%3ERegarding%207)%20I%20suspect%20the%20main%20reason%20that%20Return%20doesn't%20exit%20out%20of%20the%20function%20is%20because%20of%201).%20Without%20seeing%20your%20actual%20function%20code%2C%20it's%20hard%20to%20tell.%20Examine%20all%20your%20code%20execution%20paths%2C%20and%20ensure%20your%20real%20function%20has%20a%20legitimate%20way%20to%20exit%20gracefully%20--%20rather%20than%20going%20deeper%20into%20CFile%20with%20recursion.%20Given%20your%20simplified%20version%20posted%20here%2C%20it%20might%20be%20that%20%22catch%20%7B%20CFile%20%7D%22%20is%20the%20actual%20culprit%20why%20it%20never%20returns.%3C%2FLINGO-BODY%3E
New Contributor

I am using below code

1) To verify whether given input text is in JSON format

then

2) User acceptance on the file selected as per its version.

 

Means, if i give wrong JSON format in the input file name, it will ask for a correct formatted file so that I will verify the JSON code in file, correct it and then input again. Then it will verify the version of the JSON file and asks for user acceptance. Post user acceptance, return value otherwise ask for alternate file with required version.

 

But, lets say if i took 'n' iterations to get correct JSON format, post giving correct formatted JSON as input, user acceptance is also iterating 'n' times even though it is accepted initial time itself. Means, 'return' is not coming out of the function (which should actually come out of the function as per my knowledge). Please correct me if my assumption about 'return' is wrong or correct the code.

 

Note: By using another function for JSON validation I got the issue fixed but I don't want to follow that process.

 

Code:


function Cfile
{
param([String]$title)
try
{
$filepath = Read-host "Enter the path of the file"
$JSONformat = gc $filepath | ConvertFrom-Json -ErrorAction Stop;
Write-Host "Valid JSON format" -ForegroundColor Green
}
catch
{
Write-Host "Provided text is not a valid JSON string" -ForegroundColor Red
Cfile -title "Select Correct Config file"
}

Write-Host "Entered baseline version is" $JSONformat.version
$confirm = Read-Host "Is that correct?? (Y/N)"
Switch($confirm)
{
Y{
$done = "Good job"
Break
}
Default{
Write-Host "Select Correct Config file"
cfile -title "Select Correct Config file"
}
}
Return($Done)
}

$Required = Cfile "Select required JSON"

 

 

Input JSON file (correct format):

{
"version": 87,
"TestingJSON": [
{
"type": "string",
"category": "crytical"
}
]
}

 

Note: To give wrong JSON format as input, may be you can remove any Comma, brace, quotation .. etc from above file (if you are not much familiar about JSON.

3 Replies
Best Response confirmed by SBVRaja (New Contributor)
Solution

@SBVRaja I several gotchas which may explain the behavior you're seeing...

 

  • Using recursive calls to CFile is probably not the best approach, especially from inside the catch {} block.
  • The $Done variable is never properly initialized, and the only possible return value is "Good job" for $Required.
  • I might recommend moving any statements referencing $JSONformat inside your try {} block.
  • Your $title argument to the CFile function is never used.
  • If possible, avoid using Write-Host; and instead try using either Write-Verbose, Write-Output, Write-Information, Write-Warning, etc.
  • Since your Switch() statement only handles 2 conditions, an if() {} else {} block might be cleaner.
  • Return($Done) is mostly equivalent to: $Done; Return; Since this is the last statement in your function, Return is really unnecessary.

That said, here's one possible re-write:

 

function Cfile
{
	[cmdletbinding()]
	param([string]$title = 'Enter the path of the file')
	
	begin {
		$done = 'Bad job'
	}
	
	process {
		Do {
			try {
				$filepath = Read-host $title
				$JSONformat = (get-content -Path $filepath -ErrorAction Stop | ConvertFrom-Json -ErrorAction Stop)
				Write-Verbose "Valid JSON format`nEntered baseline version is: $($JSONformat.version)"
			} catch {
				Write-Warning 'Missing file, or provided text is not a valid JSON string'
			}

			$confirm = Read-Host 'Is that correct?? (Y/N)'
			if ('Y' -eq $confirm) {
				$done = 'Good job'
			} else {
				$title = 'Select Correct Config file'
			}
		} Until ('Bad job' -ne $done)
	
		$done
	}
	
	end {}
}

$Required = Cfile -Verbose
Write-Output $Required

 

Hope this helps.

@JRCigars Thanks for looking into this and your time JRCigars. I really appreciate that. My actual problem here is not to find alternate solutions (As mentioned, I already had an alternate solution and I also believe there are many other solutions too just like Do-While you used which is also a great approach which I didn't think). But my actual cause here is to understand the behavior of 'Return' and put a '?' at where ever I don't have a proper answer
Considering this, here are my responses to your 7 points with '?' .. :)
1) Using recursive calls to CFile is probably not the best approach, especially from inside the catch {} block.
Raja: Understood. I may agree with you on this. But WHY?
2) The $Done variable is never properly initialized, and the only possible return value is "Good job" for $Required.
Raja: I don't think it is a big issue (in fact not a issue at all). In fact, the code that i gave is very simplified version of actual bulk code. These $Done, $title, the function CFile itself are all its simplified versions.
3) I might recommend moving any statements referencing $JSONformat inside your try {} block
Raja: I completely agree. This is actually simplifying the issue completely.
4) Your $title argument to the CFile function is never used.
Raja: I don't think its of big issue (in fact not a issue at all). In actual code, I generally use this variable to display while reading $filepath in try{} block as this function 'Cfile' i use for multi purpose. In fact, the code that i gave is very simplified version of actual bulk code. These $Done, $title, the function CFile itself are all its simplified versions.
5) If possible, avoid using Write-Host; and instead try using either Write-Verbose, Write-Output, Write-Information, Write-Warning, etc.
Raja: I would consider it. I will study this alternate approach
6) Since your Switch() statement only handles 2 conditions, an if() {} else {} block might be cleaner
Raja: I agree. But in actual code, there are 3 more conditions that it actually verifies.
7) Return($Done) is mostly equivalent to: $Done; Return;
Raja: I agree. But with the use of Return why the engine is not moving out of function??

Regarding 7) I suspect the main reason that Return doesn't exit out of the function is because of 1). Without seeing your actual function code, it's hard to tell. Examine all your code execution paths, and ensure your real function has a legitimate way to exit gracefully -- rather than going deeper into CFile with recursion. Given your simplified version posted here, it might be that "catch { CFile }" is the actual culprit why it never returns.