I imported a csv file using import-csv. I only wanted the account number from the list.
I used
$Numbers = Import-Csv csv.csv |Select-Object -ExpandProperty 'Account Number' |Where-Object {$_ -ne "0"} | Out-Null
but i want enclose the account number with "'" and separate the account number with ",". Ideally the list should look like: 'account1','account2',...,'accountlast'. I could not manipulate the $ number variable like array.
Some string manipulation and a -join should be able to get this for you. Although an array which is returned from Import-Csv csv.csv | Select-Object -ExpandProperty 'Account Number' | Where-Object {$_ -ne "0"} would be considered more versatile!
$numbers = "'{0}'" -f ((Import-Csv csv.csv | Select-Object -ExpandProperty 'Account Number' | Where-Object {$_ -ne "0"}) -join "','")
Join the account numbers with ',' and then we use the format operator to enclose that into the outer single quotes.
You are very close. You can import the csv, select the object, then use a foreach to format each object, and finally write to the host with one line instead of breaks for each.
Import-Csv csv.csv | Select-Object -ExpandProperty 'Account Number' | Where-Object {$_ -ne "0"} | ForEach{"'" + $_ + "', "} | Write-Host -NoNewLine
Related
I need to sort the words in a text file and output them to a file
Function AnalyseTo-Doc{
param ([Parameter(Mandatory=$true)][string]$Pad )
$Lines = Select-String -Path $Pad -Pattern '\b[A-Za-zA-Яа-я]{2,}\b' -AllMatches
$Words = ForEach($Line in $Lines){
ForEach($Match in $Line.Matches){
[PSCustomObject]#{
LineNumber = $Line.LineNumber
Word = $Match.Value
}
}
}
$Words | Group-Object Word | ForEach-Object {
[PSCustomObject]#{
Count= $_.Count
Word = $_.Name
Longest= $_.Lenght
}
}
| Sort-Object -Property Count | Select-Object -Last 10
}
AnalyseTo-Doc 1.txt
#Get-Content 1.txt | Sort-Bubble -Verbose | Write-Host Sorted Array: | Select-Object -Last 10 | Out-File .\dz11-11.txt
it's don't work
Sort by the Longest property (consider renaming it to Length), which is intended to contain the word length, but must be redefined to $_.Group[0].Word.Length:Tip of the hat to Daniel.
$Words | Group-Object Word | ForEach-Object {
[PSCustomObject]#{
Count= $_.Count
Word = $_.Name
Longest = $_.Group[0].Word.Length
}
} |
Sort-Object -Descending -Property Longest |
Select-Object -First 10
Note that, for conceptual clarity, I've used -Descending to sort by longest words first, which then requires -First 10 instead of -Last 10 to get the top 10.
As for what you tried:
Sorting by the Count property sorts by frequency of occurrence instead, i.e. by how often each word appears in the input file, due to use of Group-Object.
Longest= $_.Length (note that your code had a typo there) accesses the length property of each group object, which is an instance of Microsoft.PowerShell.Commands.GroupInfo, not that of the word being grouped by.
(Since such a GroupInfo instance has no type-native .Length property, but PowerShell automatically provides such a property as an intrinsic member, in the interest of unified handling of collections and scalars. Since a group object itself is considered a scalar (single object), .Length returns 1. PowerShell also provides .Count with the same value - unless present type-natively, which is indeed the case here: a GroupInfo object's type-native .Count property returns the count of elements in the group).
The [pscustomobject] instances wrapping the word at hand are stored in the .Group property, and since they're all the same here, .Group[0].Word.Length can be used to return the length of the word at hand.
Function AnalyseTo-Doc{
param ([Parameter(Mandatory=$true)][string]$Pad )
$Lines = Select-String -Path $Pad -Pattern '\b[A-Za-zA-Яа-я]{2,}\b' -AllMatches
$Words = ForEach($Line in $Lines){
ForEach($Match in $Line.Matches){
[PSCustomObject]#{
LineNumber = $Line.LineNumber
Word = $Match.Value
}
}
}
$Words | Group-Object Word | ForEach-Object {
[PSCustomObject]#{
#Count= $_.Count
Word = $_.Name
Longest = $_.Group[0].Word.Length
}
} |
Sort-Object -Descending -Property Longest | Select-Object -First 10 | Out-File .\dz11-11.txt
}
AnalyseTo-Doc 1.txt
I'm trying to add a string to the emailaddresses objects of an Exchange mailbox.
But when I add the string, it doesn't show anything.
Get-Mailbox -ResultSize Unlimited | Select-Object DisplayName,PrimarySmtpAddress, #{Name="EmailAddresses";Expression={($_.EmailAddresses | Where-Object {$_ -clike "*smtp*"} | ForEach-Object {$_ -join '_OLD'})}} | where-object {($_.primarysmtpaddress -like "juan#contoso.com")}
but when I do a replace operation, it shows me the emailaddresses replaced correctly. Why doesn't it add the string but replace it?
Get-Mailbox -ResultSize Unlimited | Select-Object DisplayName,PrimarySmtpAddress, #{Name="EmailAddresses";Expression={($_.EmailAddresses | Where-Object {$_ -clike "*smtp*"} | ForEach-Object {$_ -replace "smtp:","Remp:"}) -join "OLD"}} | where-object {($_.primarysmtpaddress -like "juan#contoso.com")}
I have also tried with the concatenation operator, but I get the same result.
thanks in advance.
I have the following code to get the headers in an excel
$inputTable = Import-Excel -Path $inputFile
$sourceTable = $inputTable | Get-Member -MemberType NoteProperty | %{"$($_.Name)"}
However it is sorting by alphabet. I want the headers in the same sequence as in the excel. How should I do it?
try this :
$inputTable | get-Member -force | where Name -eq "psextended" | %{
$_.Definition.replace('psextended', '').replace('{', '').replace('}', '').Trim() -split ',' | %{$_.trim()}
}
Csv fiile should contain ResourceGroupName,FrontendIpConfigurationsName,FrontIPAddress,VMstatus.I have written a below code.Am getting the details in csv file but the issue is FrontendIpConfigurationsName starts with same name comes in same row like ersfrontend-A1,ersfrontend-B2,ersfrontend-B3,ersfrontend-D1, and also IPs respective to the names comes in different column but in same row.But I wanted them to come different rows.Please suggest
report = #()
$LBlist = Get-AZLoadBalancer | Where-Object {$_ResourceGroupName -eq '*$(grp-wildcard)' } |Select-Object
$VM =Get-AzVm -Status | Where-Object {$_ResourceGroupName -eq '*$(grp-wildcard) '} |Select-Object
$power= $VM.powerstate
foreach($LB in LBlist){
Array = "" |Select-Object ResourceGroupName,FrontendIpConfigurationsName,FrontIPAddress,VMstatus
$Array.ResourceGroupName = $LB.ResourceGroupName
$Array.FrontendIpConfigurationsName =($LB.FrontendIpConfigurationsName.name -join ',')
$Array.FrontendIpAddress= ($LB.FrontendIpConfigurationsName.privateIpAddress -join ',')
$Array.VMstate = $power
}
$report+=$Array
$report |Format-Table ResourceGroupName,FrontendIpConfigurationsName,FrontIPAddress,VMstatus
$report | Export-Csv -NTI -path "$(BuildArtifactStagingDirectory)/LBlist.csv"
In order to create a proper CSV file with headers and rows of data, you need to collect an array of Objects and send that to the Export-Csv cmdlet.
For example
$report = #()
foreach($LB in $LBlist){
$obj= [PsCustomObject]#{
'ResourceGroupName ' = $LB.ResourceGroupName
'FrontendIpConfigurationsName ' =($LB.FrontendIpConfigurationsName.name -join ',')
'FrontendIpAddress' =($LB.FrontendIpConfigurationsName.privateIpAddress -join ',')
'VMstate ' = $power
}
$report += $obj
}
$report |Format-Table ResourceGroupName,FrontendIpConfigurationsName,FrontIPAddress,VMstatus
$report | Export-Csv -NTI -path "($BuildArtifactStagingDirectory)/LBlist.csv" -NoTypeInformation
Hope that helps
I am looking for duplicates on a share drive so I can let the users know and they can clean it up before we use anything automated. My largest duplicate is close to 400 copies, but the info is all on a single line.
My query is getting the correct results:
$a = Get-ChildItem -Path "S:\" -File -Recurse |
Select-Object -Property Fullname, #{N='Hash';E={(Get-FileHash $_.FullName).Hash}}
$cnt = $a | Group-Object -Property Hash
$cnt |
Select-Object Count, #{N='FullName';E={($_.Group).FullName}}, #{N='Hash';E={($_.Group).Hash}} |
Sort-Object -Property Count -Descending |
Export-Csv C:\Temp\S_Drive_Counts.csv
Here is an example of my results where each entry is on a single line:
"Count","FullName","Hash"
"2","S:\Generation 1\Certification Authority.txt S:\Generation 2\Certification Authority.txt","498868376A5377F731593E9F96EC99F34C69F47537C81B9B32DBAC9321462B83 498868376A5377F731593E9F96EC99F34C69F47537C81B9B32DBAC9321462B83"
I need to pass this info on though, so I'd like to have each entry on a line by itself, similar to this:
"Count","FullName","Hash"
"2","S:\Generation 1\Certification Authority.txt","498868376A5377F731593E9F96EC99F34C69F47537C81B9B32DBAC9321462B83"
"2","S:\Generation 2\Certification Authority.txt","498868376A5377F731593E9F96EC99F34C69F47537C81B9B32DBAC9321462B83"
I can do some string manipulation to the CSV if needed, but I am looking for a way to get it in the correct format before exporting to the CSV.
Unroll your groups. Also, make better use of the pipeline.
Get-ChildItem -Path 'S:\' -File -Recurse |
Select-Object Fullname, #{n='Hash';e={(Get-FileHash $_.FullName).Hash}} |
Group-Object Hash |
ForEach-Object {
$cnt = $_.Count
$_.Group | Select-Object #{n='Count';e={$cnt}}, FullName, Hash
} |
Sort-Object Count, Hash, FullName -Descending |
Export-Csv 'C:\Temp\S_Drive_Counts.csv' -NoType