How to get cmdlet get-item output into a variable as a string

I mean, when I call get-item with a directory that it dumps to the console as follows

    ----                -------------     ------ ----
d----         2/16/2011   8:27 PM            2011-2-16
-a---         2/13/2011   8:24 PM 3906877184 SWP-Full Database Backup_2011-02-13
                                           0
-a---         2/16/2011   8:23 PM 3919766476 SWP-Full Database Backup_2011-02-16.bak
                                           8
-a---         2/12/2011   8:18 PM 3906877747 SWP-Full Database Backup_2011-02-12
                                           2
-a---         2/14/2011   8:21 PM 3875484467 SWP-Full Database Backup_2011-02-14
                                           2

but when I convert to a string, it changes as

\\192.168.2.89\BwLive\2011-2-16 \\192.168.2.89\BwLive\SWP-Full Database Backup_2011-02-13 \\192.168.2.89\BwLive\SWP-Full
 Database Backup_2011-02-16.bak \\192.168.2.89\BwLive\SWP-Full Database Backup_2011-02-12 \\192.168.2.89\BwLive\SWP-Full
 Database Backup_2011-02-14

i means that the attributes of length, size, time are omitted, how can I save these attributes when converting to a string?

thank.

+3
source share
2 answers

If I understand what you need, this should work:

$a = get-childitem <filespec> |
  select name,length,lastwritetime |
   format-table | out-string

Then put $ a in your inbox.

+6
source

You should explore the various options that Powershell gives you to format the results from the pipeline.

, , , .

$body = Get-ChildItem | Out-String

, Format-Table CmdLet:

$body = Get-ChildItem | Format-Table -Property Name, Length | Out-String

script :

$body = Get-ChildItem | Format-Table -Property Name, Length | Out-String

$SmtpClient = New-Object System.Net.Mail.SmtpClient
$MailMessage = New-Object System.Net.Mail.MailMessage

$SmtpClient.Host = "my.smtp.host.com"
$MailMessage.from = "sender@acme.com"
$MailMessage.To.Add("me@acme.com")
$MailMessage.Subject = "Verzeichnisinhalt"
$MailMessage.IsBodyHtml = $false
$MailMessage.Body = $body
$SmtpClient.Send($MailMessage)
+3

Source: https://habr.com/ru/post/1792734/


All Articles