Decent technique for creating string array with same value?

Welcome Forums General PowerShell Q&A Decent technique for creating string array with same value?

This topic contains 2 replies, has 3 voices, and was last updated by

 
Participant
3 months, 2 weeks ago.

  • Author
    Posts
  • #132944

    Participant
    Topics: 1
    Replies: 0
    Points: 49
    Rank: Member

    For the Start-BitsTransfer cmdlet, assuming I have a $sourcepaths array and my destination is a single directory, is this a decent technique?

    $destarray = [System.Collections.ArrayList]::Repeat('C:\TEMP\',$sourcepaths.count)
  • #133203

    Moderator
    Topics: 8
    Replies: 739
    Points: 2,071
    Helping Hand
    Rank: Community Hero

    This is cool 👍, didn't know about Repeat() method.

  • #133221

    Participant
    Topics: 2
    Replies: 397
    Points: 689
    Helping Hand
    Rank: Major Contributor

    Yep, this will get you basically the same result as other methods in PowerShell would. Nice find, I hadn't known about this one either! 🙂

    Possible alternate methods:

    $destArray = (1..$sourcePaths.Count).ForEach{ "C:\TEMP" }
    $destArray = foreach ($N in 1..$sourcePaths.Count) { "C:\Temp" }

    Both of those will get you a standard array back, instead of an arraylist, however; so if you need the arraylist at the other end, use ::Repeat()

    That said, ArrayList is effectively deprecated, so I would recommend using List[PSObject] instead... although that initial list may need to be created via a LINQ enumerable expression first, as it seems List lacks the .Repeat() method itself.

The topic ‘Decent technique for creating string array with same value?’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort