0

I'm new to Powershell, i am working with the basics. If someone could try provide an example of;

A function to get contents of a folder (get-childitem -Path c:\temp | Select-Object Name, Length) and puts them into an array of arrays -- array=@()

The script body should then output what is returned from the function. I can use a for loop to make a table format of my array which is ok.

I believe a variable scope comes in here, to return the contents of an array?

In simple terms the Function needs to do something, and outside of the function needs a variable scope to retain what get-childitem returns.

So output needs to be called outside of the function.

Example: Function - Get-childItem - Store Data in Array Call Function to Output Data in Array.

2
  • 1
    Im lost on what is wrong here. Functions will return everything that is sent to the output stream. Do you want to guarantee and array is returned? function GCI{get-childitem -Path c:\temp | Select-Object Name, Length} would return an array (if there is more that one result also depends on your PowerShell version) Commented Sep 8, 2015 at 19:43
  • yes i want to create an array in a function and then be able to call it outside the function Commented Sep 8, 2015 at 21:00

1 Answer 1

1

what you want is this then

function doSomething {get-childitem -Path c:\temp | Select-Object Name, Length}

$array = @()
$array = doSomething
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.