iceman
iceman

Reputation: 506

filenames to CSV

#get filenames from txtfile
foreach ($file in Get-Content -Path "C:\Filelist.txt") 
{
#Remove : and replace with Underscores
    $name = ( $file) -replace '\\', '_' -replace ':'
#Create new folder at destination location with new filename
    New-Item -Path "\\destinationpc\$name" -ItemType Directory -ErrorAction SilentlyContinue 
    
###Next step How would one create a csv with 2 columns, the original name and new name?
      
}

Next step I'm having difficulty with is- How would one create a csv with 2 columns, the original name and new name?

Upvotes: 1

Views: 109

Answers (1)

mklement0
mklement0

Reputation: 437176

Get-Content C:\Filelist.txt |
  ForEach-Object {
    $name = $_ -replace '\\', '_' -replace ':'
    # Create new folder at destination location with new filename
    New-Item -Path "\\destinationpc\$name" -ItemType Directory -ErrorAction SilentlyContinue 

    # Output an object with properties reflecting the original and the new name
    [pscustomobject] @{
      OriginalName = $_
      NewName = $name
    }

  } |
    Export-Csv out.csv -NoTypeInformation -Encoding utf8

Note: In PowerShell [Core] v6+, both -NoTypeInformation and -Encoding utf8 are implied; in Windows PowerShell, the default character encoding is ASCII(!), and utf8 invariably creates a UTF-8 file with a BOM.

Upvotes: 2

Related Questions