Powershell: Need to output to .csv

In my code, I am gathering user information from an OU in Active Directory. The information includes the OfficePhone number. The PhoneNum function removes +, -, ., (, ) from the number and joins all the digits into one long number.
When I run the code in the ISE, the output is exactly what I want to see:

FullName,City,State,EmailAddress,OfficePhone

However, when I try to output to csv, the file is created but is either blank, or has a single column of numbers.
I have tried out-file and export-csv. I have also tried to run the code from the command line with >> c:\test\users.csv.
Please help.

$erroractionpreference = "SilentlyContinue"
$NAUsers = Get-ADUser -SearchBase "OU=Fabrikam,DC=microsoft,DC=com" -Filter * -Properties * |Sort-Object Name |Select-Object -Property Name, City, State, EmailAddress, OfficePhone, MobilePhone |Where-object {$_.EmailAddress}
$outfile = "c:\test\ADUsersPhone.csv"

foreach($users in $NAUsers){

$something =(PhoneNum($users.officephone))

$output =@($users.Name, $users.City, $users.State, $users.EmailAddress, $something)

$output |join-string -separator "," 
}

Function PhoneNum($stringer){

#Remove "+"
if($stringer.IndexOf("+") -ge 0){
    $stringer1 = $stringer -replace "\+",""
    }
    else{
    $stringer1 = $stringer
    }
#Remove "."
if($stringer1.IndexOf('.') -ge 0){
  $stringer2 = $stringer1 -replace "\.",""
  }
  else{
  $stringer2 = $stringer1
  }
#Remove "-"
if($stringer2.IndexOf('-') -ge 0){
  $stringer3 = $stringer2 -replace "-",""
  }
  else{
  $stringer3 = $stringer2
  }
#Remove "("
if($stringer3.IndexOf("(") -ge 0){
  $stringer4 = $stringer3 -replace '\(',""
  }
  else{
  $stringer4 = $stringer3
  }
#Remove ")"
if($stringer4.IndexOf(")") -ge 0){
  $stringer5 = $stringer4 -replace '\)',""
  }
  else{
  $stringer5 = $stringer4
  }

$private:rOphone = $stringer5.Split(" ", [StringSplitOptions]::RemoveEmptyEntries)
$uOphone = [string]::Join("", $rOphone)
return $uOphone
}

Open in new window

clockwoodAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

prashanthdCommented:
Try the following..
$erroractionpreference = "SilentlyContinue"
$NAUsers = Get-ADUser -SearchBase "OU=Fabrikam,DC=microsoft,DC=com" -Filter * -Properties * |Sort-Object Name |Select-Object -Property Name, City, State, EmailAddress, OfficePhone, MobilePhone |Where-object {$_.EmailAddress}
$outfile = "c:\test\ADUsersPhone.csv"

foreach($users in $NAUsers){

$something =(PhoneNum($users.officephone))

#$output =@($users.Name, $users.City, $users.State, $users.EmailAddress, $something)

#$output |join-string -separator "," 
$output="$users.Name,$users.City,$users.State,$users.EmailAddress,$something"
$output
$output | Out-File $outfile -Append
}

Function PhoneNum($stringer){

#Remove "+"
if($stringer.IndexOf("+") -ge 0){
    $stringer1 = $stringer -replace "\+",""
    }
    else{
    $stringer1 = $stringer
    }
#Remove "."
if($stringer1.IndexOf('.') -ge 0){
  $stringer2 = $stringer1 -replace "\.",""
  }
  else{
  $stringer2 = $stringer1
  }
#Remove "-"
if($stringer2.IndexOf('-') -ge 0){
  $stringer3 = $stringer2 -replace "-",""
  }
  else{
  $stringer3 = $stringer2
  }
#Remove "("
if($stringer3.IndexOf("(") -ge 0){
  $stringer4 = $stringer3 -replace '\(',""
  }
  else{
  $stringer4 = $stringer3
  }
#Remove ")"
if($stringer4.IndexOf(")") -ge 0){
  $stringer5 = $stringer4 -replace '\)',""
  }
  else{
  $stringer5 = $stringer4
  }

$private:rOphone = $stringer5.Split(" ", [StringSplitOptions]::RemoveEmptyEntries)
$uOphone = [string]::Join("", $rOphone)
return $uOphone
}

Open in new window

prashanthdCommented:
Modify Line 12 to following

$output="$($users.Name),$($users.City),$($users.State),$($users.EmailAddress),$something"

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
clockwoodAuthor Commented:
prashanthd...

That did it! Thank you very much. If you would be so kind, could you explaing what the "$(" does to the variables?
prashanthdCommented:
1)Access a variable within ""

$name="Bill"

write-host "My name is $name"

Output - My Name is Bill

2)Access property of an object with ""

$mailbox=Get-Mailbox bill

write-host "My Email address is $($mailbox.smtpaddress)"

output - My email address is bill@abc.com
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.