►
Description
Watch Ryan Leap (Twitter: @leap_ryan) show what the PowerShell EncodedCommand parameter is and how it works. Ryan is a 20 year IT pro experience in systems administration and development and always has some cool tricks up his sleeve!
In this video Ryan explains that he often needs to support older batch files in his org. He wanted to find a way to use PowerShell to manipulate those existing batch files. What he discovered is the EncodedCommand parameter.
Ryan shows us a custom function utility he wrote that takes an encoded command parameter and produces custom code on the fly that allows him to run Powershell code as batch files.
A
Just
wanted
to
show
folks
the
encoded
command,
just
in
case
something
that
you
had
never
seen
or
haven't
used
before
it's
coming
to
handy
for
me,
I'm,
an
environment
where
a
lot
of
times
I
have
to
work
with
visting
batch
files
and
I
just
want
to
do
it
in
PowerShell,
but
maybe
I
don't
want
to
redo
the
whole
batch
file
and
I
still
want
to
use
some
PowerShell
commands.
I
found
that
being
able
to
encode
a
PowerShell
command
and
then
run
it
and
batch
really
helpful.
A
A
It
doesn't
go
out
right,
so,
what's
going
on
here?
Well,
the
command
processor
ends
up,
gobbling
up
your
quote
and
then
so
you're
passing
in
what
I
wanted
here
was
the
thing
to
be
in
quotes
to
be
in
quotes
when
I
handed
it
off
to
Power
Cells
that
it
would
evaluate
freshen
in
the
string
and
do
the
work,
but
the
command
process
for
got
in
the
way
messed
up.
My
quoting
messed
up
my
command.
A
So
if
I,
you
know
if
I
know
that
and
I
know
how
to
escape
stuff
in
in
batch
land,
I
can
I
can
get
around
that
right
works,
but
that's
kind
of
for
the
birds
right.
You
you
have
this
command
and
we
don't
even
know
if
the
command
processor
is
going
to
mess
it
up
for
you,
and
so
that's
where
the
PowerShell
team
Gabe.
A
You
gave
you
this
option:
encoded
command,
that's
the
64
encoded
string.
You
use
this
to
submit
commands
that
have
complex
quotation.
Well,
my
example:
I
wouldn't
call
it
complex
quotation,
but
was
enough
to
watch
it
break
right.
So,
let's
take
advantage
of
this.
They
give
us
an
example
down
here.
Let's
just
take
their
example,.
B
B
B
A
B
A
It
just
worked
see
it
didn't
get
tripped
up
on
any
there's,
nothing
to
get
tripped
up
on
right,
because
it's
encoded
and
so
great.
So
now
you
know
how
to
take
something
that
you've
done
in
PowerShell
and
code.
It
up
can
safely
run
it
in
in
the
batch
file
without
fear
of
it
getting
messed
with.
But
what
I
did
is
that
I
wanted
to
do
this.
More
often
so
I
wrote
this
little
utility
don't
convert
to
batch.
A
A
Work
right,
and
so
then
the
one
other
feature
I
yeah
guy
right
kind
of
kind
of
cool
right.
So
then
what
I
did
I
did
one
other
thing.
What
I
like
to
do
is
get
the
output
of
whatever
my
power
show
is
doing
and
set
it
to
a
batch
environment
dirt.
So
I
put
a
little
switch
on
on
on
this
convert
to
function
to
let
you
specify
a
batch
variable
right,
we'll
call
it
I.
A
Yeah
and
so
the
right
I'm
just
quickly
helping
make
a
batch
file
for
you
that
does
this
encoding
and
then
just
the
one
last
little
kind
of
icing
on
the
cake
I
wanted
to
show
you
is
that
I
took
somebody's
from
the
web.
They
test
for
admin
right,
like
are
you
running
as
an
elevated
right
and
so
I
downloaded
that
you
see?
I
got
the
PowerShell
script
test
for
admin
right.
There
means
some
code
right
and
what
I
can
do
is
I
can
take
that
whole
thing.
I'll
take
get
the
content
out
of
it.
A
B
A
So
so
real
quick
I
mean
going
back
to
the
whole
idea,
it's
kind
of
hard
to
test
for
an
admin
from
a
batch
file.
Right
I'm
sure
you
have
a
trick
up
your
sleeve,
but
you
don't
need
it.
Powershell
can
do
it
for
you
and
encode
it
as
long
as
your
environment.
Variable
isn't
bigger
than
the
max
size
of
the
environment.
Variable.
B
B
Like
the
scripts,
lightning
Badman
true
so.
A
Right
like
now
now
you
can
bring
this
on
scene.
You
can
bring
all
your
your
PowerShell
will
do
it
into
batch.
You
know,
and
so
that's
that's
it
just
in
my
in
my
recap.
I
would
just
say
you
know
it's
practical.
It
gives
you
an
opportunity
to
add
some
PowerShell
to
an
existing
batch
file
without
rewriting
it
it's
effective
because
it
doesn't
get
messed
up
on
quotes.
A
You
don't
have
to
worry
about
the
translation
and
it's
portable,
like
you,
can
take
this
whole
PowerShell
thing
shove
it
into
an
encoded
command
and
it
stays
with
the
batch.
So
I
mean
look
use
it
or
not,
but
it's
a
neat
trick,
Tavern
in
your
tool,
bag,
and
if
you
want
you
can
go
to
my
github
and
get
that
function.