Read from folder in Powershell

Welcome Forums General PowerShell Q&A Read from folder in Powershell

This topic contains 4 replies, has 3 voices, and was last updated by

 
Participant
2 years, 5 months ago.

  • Author
    Posts
  • #45380

    Inactive
    Points: 0
    Rank: Member

    I what I want to do is that it reads out all the folders from F: \ Test folder and writes it to C: \ temp without which it removes all folders containing al or AL. In the view itself in power shell ISE then shows the right but when it writes so take it with them al and AL

    Function GetFileNames([string]$path, [string]$outputFile) {
      $list = Get-ChildItem F:\test
        $list -Replace("al","")
        $list -Replace("AL","")
      $list |  Select-Object Name | Out-File $outputFile
    }
    GetFileNames ".\" "C:\temp\test.txt"
    

    In the output file i get this:
    AL23123
    alo2323
     
    When it really should get this:
    23123
    2323

  • #45394

    Participant
    Points: 0
    Rank: Member

    You need to use the Replace() method on $list variable rather than assigning a parameter to it (-Replace). And because PowerShell isn't case-sensitive, you only need to use it once:

    Function GetFileNames([string]$path, [string]$outputFile) {
      $list = Get-ChildItem F:\test
        $list = $list.Replace("al","") # Will replace any variation of 'al' - 'aL', 'AL', etc.
      $list |  Select-Object Name | Out-File $outputFile
    }
    GetFileNames ".\" "C:\temp\test.txt"
    • #45396

      Inactive
      Points: 0
      Rank: Member

      ok its thanks but its posible to

      $dataSource = “Server\SQL”
      $user = “Admin”
      $pwd = “Test”
      $database = “Tester”
      $connectionString = “Server=$dataSource;uid=$user; pwd=$pwd;Database=$database;Integrated Security=False;”
      
      $query = “Select ID from Costomer”
      
      $connection = New-Object System.Data.SqlClient.SqlConnection
      $connection.ConnectionString = $connectionString
      $connection.Open()
      $command = $connection.CreateCommand()
      $command.CommandText = $query
      
      $result = $command.ExecuteReader()
      
      $table = new-object “System.Data.DataTable”
      $table.Load($result)
      
      $table | Format-Table ID -Autosize| Out-File C:\Users\xxxx\Desktop\Db.txt
      $connection.Close()

      Its that posible to remove ID on top of file

    • #45994

      Participant
      Points: 1
      Rank: Member

      Hi Vijitharan, You can remove the 'ID' heading on the top of file output by using the -HideTableHeaders SwitchParameter. E.g:

      $table | Format-Table ID -Autosize -HideTableHeaders | Out-File C:\Users\xxxx\Desktop\Db.txt
  • #45743

    Inactive
    Points: 0
    Rank: Member

    Its write with AL

The topic ‘Read from folder in Powershell’ is closed to new replies.