Removing line break powershell - powershell

I am having an issue with a line break in my data. The array was made with an out-string followed by -split. If you want to see that part of the script let me know.
foreach ($item in $array) {
"_"+$item+"_"
}
Output:
_
itemname_
Desired Output:
itemname
I've tried inserting:
$item.replace('`','')
Without any change. Any ideas?

Okay, I think this should work. I was under the impression you wanted those underscores in the result.
$array -replace "`n|`r"

By default, 'Get-Content' command has the default delimiter of a new line '\n'. Create a costume parameter and then do your replace command. Hope this helps.
Get-ChildItem | Get-Content -Delimiter "~" | foreach { $_ -replace "`r|`n","" }

Well how about applying mjolinor's code at the $item level, e.g.:
foreach ($item in $array) {
$item -replace '^|$','_'
}
Although I expect the same result you are already getting, there are newlines embedded in your string.
I'm not able to setup the same condition in $array myself, maybe you could post that code.
Does this work?:
foreach ($item in $array) {
$item.Trim() -replace '^|$','_'
}

foreach ($item in $array) {
"_"+$item.Trim()+"_"
}
Should do what you want.

Related

Powershell - ForEach statement - concatenate results

I'm hoping to get some help from anyone here regarding powershell scripting.
I'm trying to see if there's a way to call all the results of the ForEach statement:
ForEach ($file in $test) {
$filepath = $path+"\"+$file
write-host $filepath
}
the write-host $filepath inside the ForEach statement returns the following:
c:\....\file1.txt
c:\....\file2.txt
c:\....\file3.txt
etc...
i'm trying to see if i can get all those results and put them into 1 line that i can use outside of the foreach statement. sort of like:
c:\....\file1.txt, c:\....\file2.txt, c:\....\file3.txt etc
right now, if i use write-host $filepath outside of the ForEach statement, it only gives me the last result that $filepath got.
hope i made sense.
thank you in advance.
Nothing easier than that ... ;-)
$FullPathList = ForEach ($file in $test) {
Join-Path -Path $path -ChildPath $file
}
$FullPathList -join ','
First you create an array with the full paths, then you join them with the -join statement. ;-)
Another variant,
$path = $pwd.Path # change as needed
$test = gci # change as needed
#(ForEach ($file in $test) {
$path + "\" + $file
}) -join ", "
You might also want to get a look at the FullName property of Get-ChildItem.
If you do (gci).FullName (or maybe gci | select FullName) you'll directly get the full path.
So if $test is a gci from C:\some\dir, then $test.FullName is the array you are looking for.

Getting last X characters from each line PowerShell

I'm trying to get the last 8 characters of every line of an array in the pipeline,
I thought this would output the last 8 characters but the output seems to be blank
foreach($line in $Texfile)
{
$line[-8..-1]-join ''
}
There's any number of ways to do this, but I opted to pipe Get-Content to ForEach-Object.
Get-Content -Path <Path\to\file.txt> | ForEach-Object {
$_.Substring($_.Length - 8)
}
In your example, you'd use $Line in place of $_ and not pipe to ForEach-Object, and instead, use the Foreach language construct as you've done.
Foreach ($Line in $TextFile) {
$Line.Substring($Line.Length - 8)
}
Try this:
foreach ($Line in $Texfile) {
$Line.Remove(0, ($Line.Length - 8))
}
That works fine. You misspelled $textfile though, with no "t". Maybe that's why you had no output.
Or (-join on the left side):
'1234567890
1234567890
1234567890' | set-content file
$textfile = cat file
foreach($line in $textfile)
{
-join $line[-8..-1]
}
34567890
34567890
34567890

Compare 2 arrays with wildcards in powershell

Say we have 2 .txt-files like so:
users.txt
user.name1
user.name2
admin.name1
admin.name2
shares.txt
\\someserver\somefolder\user.name1
\\someserver\somefolder\user.name2
\\someserver\someotherfolder\admin.name1
\\someotherserver\somefolder\admin.name2
\\someplaceelse\somefolder\no.name
I want to compare these 2 points of data to show only the rows in "shares.txt" that match a name in "users.txt". I figure we need to use some sort of wildcardsearch as the username is always included in the uncpath but will not be exactly the same, here is an example of what i have tried so far without success
$users = Get-Content ".\users.txt"
$shares = Get-Content ".\shares.txt"
foreach ($line in $users)
{
if ("*$line*" -like "$shares")
}
Write-Host "this line matches $line"
}
}
foreach ($line in $users)
{
if ($shares -like "*$line*")
{
Write-Host "$($shares | where {$_ -like "*$line*"}) this line matches $line"
}
}
to get the intended information the current value of the loop must be included in the search.
The problem in your code was that like operator only allows wild cards on the right side of the expression. So the matching must happen the other way around.

Pulling a substring for each line in file

Using Powershell, I am simply trying to pull 15 characters starting from the 37th position of any record that begins with a 6. I'd like to loop through and generate a record for each instance so it can later be put into an output file. But I seem to not be hitting the correct syntax just to return the 15 characters I know I am missing something obvious. Been at this for a while. Here is my script:
$content = Get-Content -Path .\tmfhsyst*.txt | Where-Object { $_.StartsWith("6") }
foreach ($line in $contents)
{
$val102 = $line.substring(36,15)
}
write-output $val102
Just as Bill_Stewart pointed out, you need to move your Write-Output line inside the ForEach loop. A possibly better way to do it would just be to pipe it:
Get-Content -Path .\tmfhsyst*.txt | Where-Object { $_.StartsWith("6") } | foreach{$_.substring(36,15)}
That should give you the output you desired.
Using Substring() has the disadvantage that it will raise an error if the string is shorter than start index + substring length. You can avoid this with a regular expression match:
(Get-Content -Path .\tmfhsyst*.txt) -match '^6.{35}(.{15})' | % { $matches[1] }

Powershell - error checking for find and replace (ForEach-Object)

I need to add error checking on the ForEach-Object part.
Currently, this code works to replace a value in a file. However, if it can't find the value, it doesn't seem to generate any error. I have done a try and catch but it just isn't working. I've searched for hours and tried all kinds of stuff ... any help?
I can do the command two different ways, not sure what is better or easier to add error checking on...
(Get-Content $file) | ForEach-Object { $_ -replace $replace, $with } | Set-Content $file
-OR-
ForEach-Object { (Get-Content $file) -replace $replace, $with } | Set-Content $file
Thank you.
finding nothing to replace is not an error, it just returns the original back. so check for it in an if statement, like:
foreach { $rep=$_ -replace $replace,$width; if ($_ -eq $rep) {...} }