Author Posts

April 18, 2018 at 1:57 pm

This has got to be a simple question, but I can't seem to get this right.

I have a script that copies photos from one share to my local machine, and then will upload them to AD and Azure. Often the photos are not named properly so I want to pause the script for a moment to review the names and then continue on if good or once I have manually gone in an edited the broken names. I've tried start-sleep, pause, read-host and no matter which one I use the pause happens before the list of the names.

#variables
$source = "\\server\photos"
$dest = "C:\Scripts\Photos\"
$oldphotos = "C:\scripts\Photos\old\"

#copy files from share to dest

Get-Childitem $source | Copy-Item -Destination $dest

#prompt for manual review of files to ensure they are in the proper format

Get-ChildItem $dest *.jpg | select name

Read-Host "Review names and make sure they are correct"

April 18, 2018 at 2:00 pm

The only way to pause-and-review would be to "break" the script. Start-Sleep just initiates a countdown and then proceeds.

Try adding a Write-Debug, with a message of some kind. When you run the script, that should produce a prompt. You can [S]uspend, review the contents of a variable or what's on the screen, and run "Exit" to resume.

It'd be better to capture the output of your command into a variable than just letting it dump to the screen. That'll make it easier to review.

April 18, 2018 at 3:39 pm

I couldn't get write-debug to work, but I did get write-warning to work. Had to convert the output to a string though.

As always, thanks Don!

$output = Get-ChildItem *.jpg | select name | out-string
Write-Warning -Message "Check file names and correct if needed, otherwise hit confirm to continue $output" -WarningAction Inquire

April 18, 2018 at 3:52 pm

You can avoid the Out-String by doing:

$output = Get-ChildItem -Include '*.jpg' | Select-Object -ExpandProperty Name
# short version with aliases:
$output = Get-ChildItem '*.jpg' | Select -Expand Name

April 18, 2018 at 3:57 pm

You are correct, however when doing expandproperty or dotting it out to name makes the format look bad

WARNING: Check file names and correct if needed, otherwise hit confirm to continue name.jpg name2.jpg name3.jpg name4.jpg name5.jpg

vs

WARNING: Check file names and correct if needed, otherwise hit confirm to continue
Name
—-
name.jpg
name2.jpg
name3.jpg
name4.jpg

April 18, 2018 at 4:31 pm

Ah, I see! Yep, in that case, nice find. I tend not to use a lot of Out-String myself. 🙂

April 18, 2018 at 5:00 pm

Neither do I, but it kept barking at me about not being a string 🙂