Reputation: 6391
I would like to assign a variable using a variety of methods available to an object all in one attempt. I'm not sure if this ability exist in powershell, but here is the long way to do it so that it works:
function myFunction() {
$createArray = @(Get-Content $aFile)
$arrayWithoutCharacters = $createArray.replace('-', '')
return $arrayWithoutCharacters
}
Instead, I'd like to do:
$createArray = @(Get-Content $aFile::replace('-', ''))
But I don't know enough about powershell to know why this isn't working.
Sample Input:
f1d06a1b-0470-44f1-895c-be1fd8b92e1c
75417fc3-afce-4b30-ab24-d306540c4020
2b0b2515-192d-470a-9745-4a3fb7c0203a
5498abad-2816-4d28-92cc-c08a3dfce8e6
1c2a95a3-2f44-432b-a276-ea23ece8a351
e58eeb44-4119-477f-b192-bf272054c69e
79f342ab-66e7-42a7-8315-2677f99f20c5
df2af860-52cd-48e6-aaf5-579b2455276a
92f745a2-c314-4a42-998c-dd611478b9b3
c971a1b2-937d-4e35-bea6-1cde18e9450a
fe3053c7-cc35-4e94-b176-1e2a8e9c8c14
fc3856b2-d22f-4255-a4e6-c11f508c3b99
d88e7175-ba36-4179-8589-13080120e49a
98c62b24-a3dd-4843-ad1a-e08d95624f00
bee25d62-3828-44ef-9b60-502dc4f14c2b
b889c381-b194-434f-9af8-3626b4186601
0a3f2663-1b7e-48f1-854d-f62c5469434b
Sample output that I would like :
f1d06a1b047044f1895cbe1fd8b92e1c
75417fc3afce4b30ab24d306540c4020
2b0b2515192d470a97454a3fb7c0203a
5498abad28164d2892ccc08a3dfce8e6
1c2a95a32f44432ba276ea23ece8a351
e58eeb444119477fb192bf272054c69e
79f342ab66e742a783152677f99f20c5
df2af86052cd48e6aaf5579b2455276a
92f745a2c3144a42998cdd611478b9b3
c971a1b2937d4e35bea61cde18e9450a
fe3053c7cc354e94b1761e2a8e9c8c14
fc3856b2d22f4255a4e6c11f508c3b99
d88e7175ba364179858913080120e49a
98c62b24a3dd4843ad1ae08d95624f00
bee25d62382844ef9b60502dc4f14c2b
b889c381b194434f9af83626b4186601
0a3f26631b7e48f1854df62c5469434b
Upvotes: 1
Views: 207
Reputation: 5142
EDIT: just saw your updates & other answers; looks like @Hyper Anthony beat me to it, but...
This should work for what you're trying to do:
$createArray = Get-Content $aFile | foreach {$_.Replace('-','')}
Or, more specifically, given the sample data you provided, this script:
$aFile = "C:\path\to\your\file\replace_test.txt"
$createArray = Get-Content $aFile | foreach {$_.Replace('-','')}
$createArray
Produces this output:
f1d06a1b047044f1895cbe1fd8b92e1c
75417fc3afce4b30ab24d306540c4020
2b0b2515192d470a97454a3fb7c0203a
5498abad28164d2892ccc08a3dfce8e6
...
Upvotes: 2
Reputation: 26003
This will create a collection of strings (strings are the input from Get-Content
, sent one at a time across the pipeline to Foreach-Object
where the dash character will be replaced.
$createArray = @(Get-Content $aFile | Foreach-Object { $_ -replace "-", "" })
Wrapping this in an explicit array is important, because you want consistency in the object type that will be returned. This should always return an Object[]
. Without that wrapping, a 0 or 1 line long $aFile could simply return a string
.
If you're looking to shorten the code as much as possible, you can replace some cmdlets with aliases:
$createArray = @(gc $aFile | % { $_ -replace "-", "" })
But I would generally recommend against making a habit of that in scripts (beyond the most common of aliases... for example Foreach-Object is commonly replaced by foreach
or %
), especially if other people will be reading and using them. It makes the script less readable when you come across some esoteric alias as opposed to the cmdlet's name.
Upvotes: 2
Reputation: 3784
Try this:
$createArray = $(@(Get-Content $aFile)).replace('-', '')
Upvotes: 1