Dynamically create variables in PowerShell - variables

How do I create a new variable each time a loop runs?
Something along the lines of
for ($i=1; $i -le 5; $i++)
{
$"var + $i" = $i
write-host $"var + $i
}

Use New-Variable and Get-Variable (mind available options including scopes). E.g.
for ($i=1; $i -le 5; $i++)
{
New-Variable -Name "var$i" -Value $i
Get-Variable -Name "var$i" -ValueOnly
}

Related

How can I restart the list inside of a word table using PowerShell?

So, I have been trying to iterate through a table in word, starting with the second row and 4th column. I created a macro in the document that restarts a list, "ListRestart_2023."
I tried the following and nothing happened
# Initialize Word application
$word = New-Object -ComObject Word.Application
$word.Visible = $True
Unblock-File "C:\temp\temp.docx"
$docDoc = $word.Documents.Open("C:\temp\temp.docx")
Write-Host "doc full name: " $docDoc.FullName
$docTable = $docDoc.Tables[3]
for ($i = 2; $i -le $docTable.Rows.Count; $i++) {
for ($j = 4; $j -le $docTable.Columns.Count; $j++) {
$word.Application.Run("ListRestart_2023")
}
}
#save and close the destination document
$docDoc.Save()
$docDoc.Close()
# Quit the Word application
$word.Quit()
Is there a better way to do this? I tried this too:
for ($i = 1; $i -le $docTable.Rows.Count; $i++) {
for ($j = 1; $j -le $docTable.Columns.Count; $j++) {
$listParagraphs = $docTable.Cell($i,$j).Range.ListParagraphs
foreach($listParagraph in $listParagraphs)
{
$listParagraph.Range.ListFormat.RestartNumbering()
}
}
}
And this,
$docTable.Cell($i,$j).Range.ListFormat.RestartNumbering()
But, I haven't been able to get anything to happen.

PowerShell Change Variable in ScriptBlock

I'm trying to change a variable inside a ScriptBlock.
What am I doing wrong?
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load($reader)
$Window.Add_SourceInitialized( {
$timer = new-object System.Windows.Threading.DispatcherTimer
$timer.Interval = [TimeSpan]"0:0:0.25"
$timer.Add_Tick( $updateBlock )
$timer.Start()
} )
$count = 0
$updateBlock = { Write-Host $count; $count++; Write-Host $count}
The Output is a repeating sequence of 0 and 1. So how do I access the variable and not only a copy of it?
When you modify $count inside the scope of the ScriptBlock, a local copy is created, and the original $Count variable in the parent scope is left untouched.
There are a few ways to modify $count in the parent scope, either with an explicit scope qualifier:
$updateBlock = { Write-Host $count; $script:count++; Write-Host $count}
Or by retrieving the variable with Get-Variable and the relative -Scope parameter (-Scope 1 refers to the immediate parent scope):
$updateBlock = { Write-Host $count; (Get-Variable -Scope 1 -Name count).Value++; Write-Host $count}
Or (as pointed out by #PetSerAl), use the [ref] keyword:
$updateBlock = { Write-Host $count; ([ref]$count).Value++; Write-Host $count}

Powershell automatic variables

So I got this code :
$t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10, $t11 = $list[0].split(" ")
but is too much, and I think I can automate the creation of $t variables.
How is the correct procedure? with loop but to have the same result.
for ($i=0;$i -le 21; $i++) {
$objResult += New-Object -TypeName PSObject -Property #{"C$i" = ($list[0].split(" "))[$i]}
}
or with
set-variable -name "T$i" -value ($list[0].split(" "))[$i]
If these are simple variables, Set-Variable works perfectly. like so:
for ($i = 0; $i -lt 3; $i++) {
set-variable -name "test$i" -value $i
}
$test0
$test1
$test2
PS > .\test.ps1
0
1
2
If you are looking to work with somewhat more complicated variables (such as COM objects for instance) then New-Object is what you need.

Creating Multiple folders in sharepoint 2010 custom list at a time

I have a custom list with name "Customers". Client having around 900 customers. He wants to create a folder for every customer. he has given customers list to me in a Excel sheet. How can we create multiple folders at a time or easily ( not creating folders one by one).
One more thing is he wants to give folder level permission to every customer on their respective folder.
How can we achieve this using code or OOB or Designer. Please help me..
Thanks & Regards,
Prasad Kommuru
Take a look at this.
$spAssignment = Start-SPAssignment
$mylist = (Get-SPWeb -identity http://portal.sharepoint.com -AssignmentCollection $spAssignment).Lists["LargeList"]
for($i=1; $i -le 10; $i++)
{
$folder = $mylist.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder)
$folder["Title"] = "folder$i"
$folder.Update();
for($j=1; $j -le 10; $j++)
{
$s1folder = $mylist.AddItem($folder.Folder.ServerRelativeUrl,
[Microsoft.SharePoint.SPFileSystemObjectType]::Folder)
$s1folder["Title"] = "subfolder$j"
$s1folder.Update();
for($k=1; $k -le 10; $k++)
{
$s2folder = $mylist.AddItem($s1folder.Folder.ServerRelativeUrl,
[Microsoft.SharePoint.SPFileSystemObjectType]::Folder)
$s2folder["Title"] = "subsubfolder$k"
$s2folder.Update();
for($l=1; $l -le 50; $l++)
{
#Create item
$newItem = $mylist.AddItem($s2folder.Folder.ServerRelativeUrl,
[Microsoft.SharePoint.SPFileSystemObjectType]::File, $null)
$newItem["Title"] = "Item $i $j $k $l"
$newItem["FirstName"] = "FirstName $i $j $k $l"
$newItem["LastName"] = "LastName $i $j $k $l"
$newItem["Company"] = "Company $i $j $k $l"
$newItem.Update()
}
}
}
}
Stop-SPAssignment $spAssignment
Hope this helps. Try the below options
http://www.c-sharpcorner.com/Blogs/10552/
or
http://sharepoint-tutorial.net/post/2011/03/21/sharepoint-2010-programmatically-create-a-folder-in-document-libraries-and-lists.aspx

Maya MEL scripting sprintf equivalent

How do you print an integer into a string in Maya MEL scripting?
It turns out you can just use + to concatenate string and integer objects in Maya Embedded Language.
For example:
int $i ;
string $s ;
for( $i = 0; $i < 5; $i++ ) {
$s = "ooh" + $i ;
print $s ;
}
There is also the format command