Read from folder in Powershell

This topic contains 4 replies, has 3 voices, and was last updated by  Trevor Freedland 1 year, 11 months ago.

  • Author
  • #45380


    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:
    When it really should get this:

  • #45394

    Aaron Hardy

    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


      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
      $command = $connection.CreateCommand()
      $command.CommandText = $query
      $result = $command.ExecuteReader()
      $table = new-object “System.Data.DataTable”
      $table | Format-Table ID -Autosize| Out-File C:\Users\xxxx\Desktop\Db.txt

      Its that posible to remove ID on top of file

    • #45994

      Trevor Freedland

      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


    Its write with AL

You must be logged in to reply to this topic.