Advertisement
pshelp

Update Arrays In Powershell

Feb 12th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#-->The first 4 lines are the same for all attempts.  Between attempts I exited PowerShell.
  2. I know I could use the Remove-Variable command too.<--#>
  3. $Date = Get-Date -DisplayHint Date
  4. Get-ADUser -Filter * -SearchBase "OU=Users,DC=Domain" -SearchScope OneLevel -Properties sAMAccountName |
  5. Select-Object @{Name="User ID";Expression={$_."sAMAccountName"}}, @{Name="Date";Expression={$Date}} |
  6. Export-Csv "C:\File.csv" -NoTypeInformation
  7.  
  8.  
  9. #-->First Attempt<--#
  10. $Import = Import-Csv C:\File.csv
  11. $Sample = Get-Random -InputObject $Import -Count 20
  12. $List = $Import -ne $Sample
  13.  
  14. #This shows 20.
  15. $Sample.Count
  16.  
  17. #These both show the same number instead of $List being 20 less than $Import.  
  18. $Import.Count
  19. $List.Count
  20.  
  21.  
  22. #-->Second Attempt<--#
  23. $Import = Get-Content C:\File.csv
  24. $Sample = Get-Random -InputObject $Import -Count 20
  25. $List = $Import -ne $Sample
  26.  
  27. #This shows 20.
  28. $Sample.Count
  29.  
  30. #These both show the same number instead of $List being 20 less than $Import.  
  31. $Import.Count
  32. $List.Count
  33.  
  34.  
  35. #-->Third Attempt<--#
  36. [System.Collections.ArrayList]$Import = Import-Csv C:\File.csv
  37. [System.Collections.ArrayList]$Sample = Get-Random -InputObject $Import -Count 20
  38. [System.Collections.ArrayList]$List = $Import -ne $Sample
  39.  
  40. #This shows 20.
  41. $Sample.Count
  42.  
  43. #These both show the same number instead of $List being 20 less than $Import.  
  44. $Import.Count
  45. $List.Count
  46.  
  47. #-->Fourth Attempt<--#
  48. [System.Collections.ArrayList]$Import = Import-Csv C:\File.csv
  49. [System.Collections.ArrayList]$Sample = Get-Random -InputObject $Import -Count 20
  50. [System.Collections.ArrayList]$List = $Import.Remove($Sample)
  51.  
  52. #This shows 20.
  53. $Sample.Count
  54.  
  55. #This shows 1000.
  56. $Import.Count
  57.  
  58. #This shows 0.
  59. $List.Count
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement